Skip to content

Instantly share code, notes, and snippets.

@jeremyclark13
Created November 16, 2012 19:26
Show Gist options
  • Select an option

  • Save jeremyclark13/4090126 to your computer and use it in GitHub Desktop.

Select an option

Save jeremyclark13/4090126 to your computer and use it in GitHub Desktop.
Include templates from plugin
define( 'PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
//Template fallback
add_filter( "template_include", 'ps_theme_redirect' );
function ps_theme_redirect( $template ) {
//cpt is the slug of the custom post type registered
if ( get_post_type() == 'cpt' ) {
if ( is_single() ) {
//looks in the current theme directory for the template if so it is returned otherwise the template from the plugin directory is returned.
if ( file_exists( get_stylesheet_directory() . '/single-cpt.php' ) )
return get_stylesheet_directory() . '/single-cpt.php';
return PLUGIN_PATH . '/templates/single-cpt.php';
} else {
if ( file_exists( get_stylesheet_directory() . '/archive-cpt.php' ) )
return get_stylesheet_directory() . '/archive-cpt.php';
return PLUGIN_PATH . '/templates/archive-cpt.php';
}
}
return $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment