(By “Page Template,” I mean a theme file with the “Template Name” header that can be selected in the “Template” dropdown field on the page admin.)

In multiple instances, I’ve built page templates that could have been accomplished by hooking to the_content or pre_get_posts or something similar. That’s lead me to wonder whether there’s a way to “register” a selectable theme template in functions.php or a plugin without creating the theme file itself.

Reasons why I want to do this:

  • In the scenarios I have in mind, I’m usually just copying page.php almost verbatim. That means any future change to page.php needs to get made twice. This is even more of a pain with updates to child themes.
  • I like the “Template” field UI. It makes sense to my clients more than some of the alternatives (see below) that I can think of.

In my case, I can think of some workarounds like using a custom meta box or a shortcode (to insert content that’s otherwise held in a template). I might even be able to do this (which I just thought of but haven’t tested yet):

/*
Template Name: My Template
*/

get_template_part( 'page' );

But in the end, I keep coming back to this idea, so I’m curious whether anyone knows how to do it. If you think it’s a horrible idea, I’m interested in that opinion too.

=======

UPDATE:
I should add that:

  • I know that there’s a $templates array for each theme from this support forums thread which is essentially doing the opposite of what I want to do.
  • I know that there’s a hidden custom field _wp_page_template from this WPSE thread.

I guess I just need to understand how the templates array and custom fields work together (and I’m also fuzzy on how the hidden custom meta fields work).

=======

UPDATE II: 10/8/12
The theme backend has been significantly reworked since this question was asked so the above $templates array no longer exists.

3 s
3

Pages are generally speaking rather inflexible, you would be better off using a custom post type. Also hooking into the actual page template dropdown would very likely be problematic (I doubt it would work without some serious hacking around). You’re better off just writing your own meta box dropdown.

I think your on the the right track with get_template_part. It’s easy, flexible and really helps with structuring complex output, and using both parameters helps. It arguably easier to copy/paste chunks of code into templates then writing all sorts of whacky functions, and it makes more sense for other people or when you re-visit the code down the road.

The other alternative is someone similar but at the function level, using the template_redirect action.

You could have functions based on your meta selection that loads unique templates, actually now that I think about it you could probably combine it with get_template_part. Code below is not tested.

Something like:

function my_special_template() {
//page "contact" for example, make this your meta dropdown selection
    if (is_page('Contact')){  
        get_template_part( 'special-chunk' );  //file named special-chunk.php
    }
}

add_action('template_redirect', 'my_special_template');

At the end of the day you still grabbing some sort of .php file though.

Leave a Reply

Your email address will not be published. Required fields are marked *