Skip to content

Instantly share code, notes, and snippets.

@BinaryMoon
Created April 28, 2021 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMoon/879644c2b873d3632fac5cabf794d760 to your computer and use it in GitHub Desktop.
Save BinaryMoon/879644c2b873d3632fac5cabf794d760 to your computer and use it in GitHub Desktop.
Create a plugin that loads template files from a plugin
<?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