(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.