Created
November 16, 2012 19:26
-
-
Save jeremyclark13/4090126 to your computer and use it in GitHub Desktop.
Include templates from 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
| 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