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.

I am working on a site that will have up to 30 templates in it.

I don't want to have all those templates at the root of my theme folder cluttering up everything.

I know how I can modify the get_page_templates() function in the file wp-admin/includes/theme.php to make it scan sub dirs in my theme folder. But I do not want to change core files manually - how can I apply a filter to get_page_templates() with a filter that I put in functions.php ?

As the code you see below is my first attempt at this but it doesn´t work yet when I put the code inside function.php.

I can however put these changes into the core file wp-admin/includes/theme.php but that is not good practice and should always be avoided right, as auto update can wipe it out.

function get_page_templates_override() 
{
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$page_templates = array();

if ( is_array( $templates ) ) {
    $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );

    foreach ( $templates as $template ) {
        $basename = str_replace($base, '', $template);

        // modifide this by commenting it out so that sub dirs arent blocked
        // if ( false !== strpos($basename, '/') )
        //  continue;

        // modified this with FILE_USE_INCLUDE_PATH so sub dirs get scanned
        $template_data = implode( '', file( $template, FILE_USE_INCLUDE_PATH ));

        $name = '';
        if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
            $name = _cleanup_header_comment($name[1]);

        if ( !empty( $name ) ) {
            $page_templates[trim( $name )] = $basename;
        }
    }
}
return $page_templates;
}
add_filter('get_page_templates','get_page_templates_override', 1);
share|improve this question
    
Ahmmm.. you're applying a filter to an non-existing filter hook 'get_page_templates'. –  Peter Mar 11 '11 at 21:11
    
Yes I´ve been suspecting that there is no hook in the wp core system for this is that true? –  MrStack Mar 11 '11 at 21:12
    
Yeah, get_page_templates is 'just' a function without filters. –  Peter Mar 11 '11 at 21:16
    
Hmm.. am trying to see where you could plug into a filter but aint really seeing where that would be possible.. –  Peter Mar 11 '11 at 21:20
    
Carrington theme framework does some very interesting things with template hierachy, maybe it would be worth looking there? –  Dalton Mar 11 '11 at 21:25

3 Answers 3

There are a couple of tickets on this: #13265 and #11216.

Also, it looks less cluttered if you prefix each template with a fixed string:

template-a.php
template-b.php

etc.

share|improve this answer
    
Thnx for the response I ve already done this and that helps you can also move most tempalates parts into sub folders by using get_template_part('I put path to sub folder here', 'and file name here'). Pages can also be move into sub folders but to get them to show in admin page attributes I must modifiy the core file I pointed to above and that I want to avoid. –  MrStack Mar 11 '11 at 21:48
    
Or you could make your own page attributes metabox. It results in some code duplication, but at least it keeps core clean. –  scribu Mar 11 '11 at 21:50
    
Good point If I render my own meta box and call my get_pages_templates_own function instead there is no worries ;-) I will try that instead thnx =) –  MrStack Mar 11 '11 at 21:52

That part is extremely rigid, see this question about loading page templates from plugin directory.

With current state of related code I would not bother spending time on this. Especially since you want it for convenience (putting template in sub-directories) and not functionality (like loading from plugin folder).

share|improve this answer

It's worth mentioning that as of WP 3.4 (nearly a year ago) it is now possible to place template files into a directory below the theme root and WordPress will detect and use them as needed.

As per http://nacin.com/2012/03/29/page-templates-in-subdirectories-new-in-wordpress-3-4/.

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.