Hi, I am trying override/unset parent theme templates in Understrap child theme. Does anyone have a filter or function that works? I don't want users to be able to use 'blank.php' or apply sidebars by changing the 'Template' select menu in Page Attributes in the WP admin.
I have tried numerous blog posts and tutorial - all of which have not worked.
Any help would be appreciated.....
Hi, have you tried
add_filter( 'theme_page_templates', 'child_remove_page_templates' );
function child_remove_page_templates( $templates ) {
unset( $templates['page-templates/right-sidebarpage.php'] );
unset( $templates['page-templates/left-sidebarpage.php'] );
unset( $templates['page-templates/both-sidebarspage.php'] );
unset( $templates['page-templates/blank.php'] );
return $templates;
}
not tested but docs says it's $page_templates not $templates
https://developer.wordpress.org/reference/hooks/theme_page_templates/#changelog
@0dp is right. While $templates works (for me), you should use
add_filter( 'theme_page_templates', 'child_remove_page_templates' );
function child_remove_page_templates( $page_templates ) {
unset( $page_templates['page-templates/right-sidebarpage.php'] );
unset( $page_templates['page-templates/left-sidebarpage.php'] );
unset( $page_templates['page-templates/both-sidebarspage.php'] );
unset( $page_templates['page-templates/blank.php'] );
return $page_templates;
}
which is the docs compliant way to do it.
Thanks everyone - Code works like a dream.
Most helpful comment
Thanks everyone - Code works like a dream.