I am developing a simple child theme based on twentyeleven. But i notice twentyeleven has a showcase and sidebar template when you add a new page. How can i disable these templates from functions.php?
I want to show only the templates from the child theme
regards
As @toscho mentioned it is tricky. But it is doable.
Basically you need to find action hooks called before and after the template dropdown is generated and call
ob_start()
to initiate output buffering andob_get_clean()
to capture the buffered HTML. The'submitpage_box'
and'edit_page_form'
hooks will work for this, respectively.After you’ve captured the buffered HTML you need to get the list of child page templates which unfortunately requires you duplicate a lot of code from WordPress because core doesn’t make make the code reusable to get the list of page templates for only the child template. You can find the code needed in the method
get_child_page_templates()
in the code below which is almost completely a composition of the needed code from WordPress core.Once you have both the buffered HTML you need to get the list of child page templates required to remove the
<options>
s from the<select>
that are not page templates from the child theme but being sure to include the'default'
option. You can do this through a complex pair of regular expressions, thepreg_match()
andpreg_match_all()
functions and aforeach()
loop where you add only the HTML for the child theme’s templates to your list and then merge them back into the HTML that came before and after the list of<option>
sPackage all that up into a called I called
Omit_Parent_Theme_Page_Templates
which you can drop into your theme’sfunctions.php
file or include in a plugin your are building and viola, gone are the parent page templates.Here’s the source code for my
Omit_Parent_Theme_Page_Templates
class that you need:Because WordPress doesn’t provide hooks for what you want this code is much more complex than we’d like. It uses a technique â the modification of HTML captured in the output buffer â that is generally “a hack.” But given the nature of what we’re changing I don’t think it’s very likely to break unless WordPress rearranges the
id
orname
attributes of the<select>
or fundamentally changes how page templates work, neither of which are very likely.Of course if some other plugin decides to modify page templates too using the same technique but in an incompatible way then that plugin might break this code or vice versa.
But I doubt any of that is too likely and if you are using for your own site I would not worry about it at all however I would be slightly more worried about releasing the code in a widely distributed plugin, but only slightly. 🙂
UPDATE:
Ah what the heck. Decided to turn this into a plugin and publish this in the WordPress plugin repository, click here to access it. I guess I wasn’t worried enough not to publish it. 🙂
Since 3.9 you can do it easily, using the
theme_page_templates
filter.You just need to use something like this:
Further reading:
To generate the dropdown, WordPress calls
page_template_dropdown()
, which callsget_page_templates()
, which (since WP 3.4 at least) callswp_get_theme()->get_page_templates()
(i.e.WP:Theme::get_page_templates()
for the active theme).The latter method loops through the actual theme
.php
files to find any file with aTemplate Name:
header and caches the result. No actions or filters are provided at any point so far.As an alternative to Mike’s answer, we could modify the cache. Less hackish than regex-ing the output, but still hackish. Lets hook it onto the
admin_head-post.php
action, so it only runs when the post editor is shown.In the example below the template
page-home.php
is removed from the list.For your specific problem, where you want to remove the parent theme’s templates from the list, luckily there’s a public method
WP_Theme::parent()
that fetches the theme’s parent as anotherWP_Theme
object. The following (untested) code should do the trick:Note: since a removed template can no longer be chosen from the dropdown, updating/publishing an existing page that previously used the template will reset the page’s template.