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?

2 Answers 2


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;
  • This is pretty good, although the sort of thing I'm after is to manipulate the directory in which custom page templates can be found.
    – Daryl
    Commented Oct 5, 2011 at 0:39
  • Sadly That is not possible without hacking core files, your best bet would be to check is a page template was selected for a page using something like: get_post_meta($post->ID, '_wp_page_template', true); and match that in your conditional check to a custom location hosted page template. Or another thing you can do is replace the page template metabox all together and create your own which will pull the list from your custom folder.
    – Bainternet
    Commented Oct 5, 2011 at 7:57

I wanted the same functionality as this. It seemed as though it is impossible to do this without core changes, but i managed to get it working - in a slightly different way.

Create a page template in the root of your Theme, call it themename-page-loader.php

In that file put the following code:

 * Template Name: ThemeNAME Page Loader

global $post;

// Define the post_name and the file name of the current page
$pslug = $post->post_name;
$pname = $pslug . '.php';

    // Load the TEMPLATE file in your custom directory
    require_once( TEMPLATEPATH . '/views/' . $pname );

Then in your theme root, create a folder for your pages, in this example i called mine views. Inside this folder you put your page templates, just make sure the file name is the same as the page name you set in WP_ADMIN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.