I’m not even sure this is possible but before I start thinking up crazy solutions, I’ll see if there is a WordPress way to do it.
Here’s the deal.
I have a timeline jQuery plugin all set up on WordPress and I want it to be callable in a child theme through the add_theme_support method:
add_theme_support( 'timeline' );
Call it an obsession with modular design. I’ve probably read too much Tadlock for any one man’s sanity but it’s working for me.
So the first part works just fine.
I’ve got the stylesheet & js file all conditionally enqueued if theme support has been specified and the one nagging problem is that I can’t conditionally include a new page template. Having a “Timeline” template show up when timeline support is disabled is just bad UX design.
So here’s the question – is there a filter method for conditionally inserting a global page template from outside of the usual locations? For example, if I were to put my page template in /library/inc/conditionals/page-timeline.php (effectively hiding it from WordPress), how could I filter it back into the template list on a page? If I can figure that out, I can add the filter to my add_theme_support function, angels will fly, trumpets will sound, and I will be happy.
The problem is that I’m not familiar with that particular drop box or how to invoke it in an unusual way.
Thanks for your brains! (not-in-a-zombie-way)
OK, so it all comes to finding proper filter to modify the list of available templates…
The list of templates comes from
get_page_templates
function, but there are no filters in it’s code. But… It uses methodget_page_templates
of classWP_Theme
and this one will come handy, because there is a filter applied in there:So, as you can see, you can filter that list based on
$post_type
and current$post
.So what now?
You can try two different ways to do this:
I think the second way is more secure. I’m not sure what problems will occur, if the page template wont’t be just a file name and will contain path… It should work, I guess, but…
Anything else to remember?
As Mark pointed out, it will of course only filter the list of templates you can select from. If the template is already selected, given page will use it even, if you remove it from the list.
If you want to prevent pages from using this template when it’s not available, you can use
template_include
to override it…Add this code in your functions.php file that provide Select page template box in page edit