5

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)

3
  • Some helpful information about how to manipulate the WordPress template dropdown: wordpress.stackexchange.com/questions/96426/… So basically you could place kind of wrapper template inside your theme, which calls your timeline via get_template_part . Just my thoughts as starting point. – rfrq Jul 18 '13 at 11:10
  • Thanks rfrq! Justin Tadlock gave me a typically incredibly complete answer over on his forums at themehybrid.com. The short version is that they have been waiting to push this into the core for three years: core.trac.wordpress.org/ticket/13265 -- and at present there isn't a hook for this. The best answers had to do with a function that conditionally removes a template from a child theme. – Imperative Ideas Jul 19 '13 at 15:24
  • For anyone who needs to remove a template in a child programatically, the rather cumbersome function to do so is here: wordpress.stackexchange.com/questions/96426/… – Imperative Ideas Jul 19 '13 at 15:36
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...

6
  • while you might be able to filter it that way, it is just the UI and you do not prevent their use. For the other part of the question about placing them in different location, I do not think it is possible. I think the OP generally did not understand the concept of child themes... child themes are slight modification of a parent theme, using a parent theme as a framework is complicating something that needs to be simple – Mark Kaplun Apr 29 '18 at 6:48
  • I think OP wants to hide them and not force to use them. Of course you can do that too - just use template_include filter and check, if usage of given template is allowed. – Krzysiek Dróżdż Apr 29 '18 at 6:52
  • What i meant is that different locations for page templates might be problematic (wordpres will not know where to look for them), but been a while since I looked at that code, so maybe it is not the case anymore. – Mark Kaplun Apr 29 '18 at 7:08
  • ... OP want people to write complex code that needs relatively deep understanding of wordpress coding principals. It sounds to me as too complex for what child themes supposed to be – Mark Kaplun Apr 29 '18 at 7:09
  • @MarkKaplun That's why I suggested that hiding templates from the list is better solution than putting them in some other locations. But to be honest, WP uses locate_template to include them, so there is a chance that it may work... – Krzysiek Dróżdż Apr 29 '18 at 7:09
-1
add_theme_support( 'timeline' );

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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.