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…