Created
April 28, 2021 22:22
-
-
Save BinaryMoon/879644c2b873d3632fac5cabf794d760 to your computer and use it in GitHub Desktop.
Create a plugin that loads template files from a plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Grandchild Theme | |
* Description: Load template files from a plugin. | |
* Author: Ben Gillbanks | |
* Version: 1.0.0 | |
* Author URI: https://prothemedesign.com | |
* | |
* @package grandchild | |
*/ | |
/** | |
* Search for templates in plugin 'templates' dir, and load if exists. | |
* | |
* Add a templates directory to the plugin directory and add your template files | |
* to that, using the normal WordPress template heirachy. | |
* | |
* @param string $template The template to search for. | |
*/ | |
function grandchild_template_include( $template ) { | |
$filename = sprintf( | |
'%1$s/templates/%2$s', | |
untrailingslashit( plugin_dir_path( __FILE__ ) ), | |
basename( $template ) | |
); | |
if ( file_exists( $filename ) ) { | |
$template = $filename; | |
} | |
return $template; | |
} | |
add_filter( 'template_include', 'grandchild_template_include', 11 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment