WordPress Custom Page Template in a different directory

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;
}

Leave a Comment