Is it possible to have a custom page template in a different folder?
I’m setting up a little framework for WordPress that I can use over and over for themes and just for the sake of being tidy I want to put custom page templates inside a different folder rather than the root of the theme directory.
How would one go about this?
WordPress uses get_page_template()
to determine the template file to use which can be altered by filters:
//for pages
add_filter( 'page_template', 'My_custom_page_template' );
function My_custom_page_template( $page_template )
{
if ( is_page( 'my-custom-page-slug' ) ) {
$page_template="pathto/custom-page-template.php";
}
return $page_template;
}
//for posts
add_filter( 'single_template', 'My_custom_page_template' );
function My_custom_page_template( $single_template )
{
if ( is_single( 'my-custom-page-slug' ) ) {
$single_template="pathto/custom-post-template.php";
}
return $single_template;
}