While developing a website with WordPress, you’ve probably been or will be in the following situation:
You created a Child Theme, inheriting a number of page templates from the parent theme. The thing is, maybe you don’t want some of those page templates. You don’t want your users to select it, or you’re not gonna support it, or it’s not fully compatible with the modifications you’ve made, or you just simply don’t like it. Whatever the reason is, you’d like to remove it from the dropdown in the page editor when creating a new page.
Before WordPress 3.9, there were some bizarre and pretty much complicated things to accomplish this. However, wlth the introduction of the theme_page_templates
filter, it has become a really easy task to do.
So let’s say you want to remove a page template called “Full Width”, and the corresponding file is named template-full-width.php
. In order to do this, you just need to add some code like the following in the functions.php
file of your Child Theme:
<?php add_filter( 'theme_page_templates', 'child_theme_remove_page_template' ); /** * Remove page templates inherited from the parent theme. * * @param array $page_templates List of currently active page templates. * * @return array Modified list of page templates. */ function child_theme_remove_page_template( $page_templates ) { // Remove the template we don’t need. unset( $page_templates['template-full-width.php'] ); return $page_templates; }
So that’s it. Hope it helps 🙂
Further reading
- theme_page_templates (in French)
- WordPress Core changeset 27297
Hey Andrés,
Thanks for posting this solution. I’ve copied it & replaced the template name but it’s not working for me. Can you think of any situations where this might not work?
Many thanks 🙂
Hi Meagan,
I wouldn’t know it without looking at your code, but I’d check what are the names of the templates inside the
$page_templates
array. I’m assuming your version of WordPress is equal or higher than 3.9, where this feature was introduced.Best,
Andrés.
Hi Andrés,
Thanks for your reply. I ended up finding a solution on the theme_page_templates page. It’s the same solution as yours, I just also needed arguments/priority in order for it to work:
`add_filter( ‘theme_page_templates’, ‘child_theme_remove_page_templates’, 20, 3 );
function child_theme_remove_page_templates( $page_templates, $this, $post ) {
unset( $page_templates[‘page-creature.php’] );
unset( $page_templates[‘page-home.php’] );
return $page_templates;
}`