Include a page template along with add_theme_support()

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.

Read More

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)

Related posts

2 comments

  1. 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 method get_page_templates of class WP_Theme and this one will come handy, because there is a filter applied in there:

    return (array) apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );
    

    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:

    • hide page template from WP and use filter to add it
    • add normal page template and use filter to remove it

    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…

  2. add_theme_support( 'timeline' );
    

    Add this code in your functions.php file that provide Select page template box in page edit

Comments are closed.