Skip to content

Instantly share code, notes, and snippets.

@richjenks
Created December 15, 2016 17:02
Show Gist options
  • Save richjenks/070a04aabe2e3d263ea4d28b3e14c08c to your computer and use it in GitHub Desktop.
Save richjenks/070a04aabe2e3d263ea4d28b3e14c08c to your computer and use it in GitHub Desktop.
Specify a template for a post type while storing the template file outside of a theme
<?php
/**
* Allows you to specify a template for a post type while storing the
* template file outside of a theme.
*
* @param string $file Full path to template file
* @param string $post_type Post Type to use template
*/
function set_single_template( $file, $post_type ) {
add_filter( 'single_template', function( $single ) use ( $file, $post_type ) {
if ( $GLOBALS['post']->post_type === $post_type ) {
if ( file_exists( $file ) ) {
return $file;
}
}
return $single;
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment