Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Oct 4 '11 at 21:38

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 1 down vote accepted

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;
}
share|improve this answer
    
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 Oct 5 '11 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 Oct 5 '11 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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