Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active March 15, 2019 15:30
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 davidsword/671fd4c1ec20678e8ef4663f5323494f to your computer and use it in GitHub Desktop.
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/.
<?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