Last active
March 15, 2019 15:30
-
-
Save davidsword/671fd4c1ec20678e8ef4663f5323494f to your computer and use it in GitHub Desktop.
WordPress - template include file for theme from a location. Allows things like custom post types single-posttype.php to be in /myplugin/.
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 | |
/** | |
* Use a plugins template file, unless one exists in theme. | |
* | |
* Useful when creating custom post types, allowing themes to create | |
* their own template. | |
*/ | |
add_filter('template_include', function ( $template ) { | |
if ( is_singular( 'location' ) ) { // `location` is a custom post type | |
$theme_files = array('single-location.php', 'myplugin/single-location.php'); | |
// check if exists in theme | |
$exists_in_theme = locate_template($theme_files, false); | |
if ( $exists_in_theme != '' ) { | |
return $exists_in_theme; | |
} else { | |
// no, than use plugins. | |
return plugin_dir_path(__FILE__) . 'single-location.php'; | |
} | |
} | |
return $template; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment