Templates can be added by users, plugins, parent- and child themes.
- Templates added by users can be deleted by any other user with the required capabilities, by going to Appearance > Editor > Templates.
- Theme templates can be removed by deleting the HTML files in the theme’s
templates
folder.
But there are scenarios where this is not applicable:
- If the theme is not your own, and you are relying on updates from the theme author, then you should not make changes to the theme files, because your changes will be undone when the theme is updated.
- When you are using child theme and you want to remove parent theme templates, not only override them.
- If you have a plugin active that adds block templates that you don’t want to use.
So in this lesson I will describe two ways to solve it using PHP filters.
How to use a PHP template instead of a block template
I have mentioned that when you use a block theme, WordPress will look for the template in the database first, then the HTML file, and finally the PHP file. So if a parent theme or plugin is including a matching HTML file, your PHP file will be ignored.
If you only care about using the PHP file on the front of the website, you can use the template_include hook. This basic example shows how to use a custom PHP-based category template:
add_filter( 'template_include', function( $template ) {
if ( is_category() ) {
return get_theme_file_path( 'category.php' );
}
return $template;
}, 99 );
The reason why the example uses a late priority (99), is to make sure the filter is applied after other existing template filters, otherwise, it might not be removing the template! This is good to remember if you need to troubleshoot your block templates.
How to remove block templates from both the Site Editor and the front
It can be confusing to have templates available in the Site Editor, if those templates are not used to display what is on the front. With the get_block_templates hook, templates from plugins and themes can be hidden both in the Site Editor and the front.
This filter does not remove user created templates, and does not prevent users from creating them.
It loops through the templates, and then compares the slugs and removes the matching template from the list.
function prefix_disable_block_templates( $templates ) {
foreach ( $templates as $key => $template ) {
if ( $template->slug === 'category' ) {
unset( $templates[ $key ] );
}
}
return $templates;
}
add_filter( 'get_block_templates', 'prefix_disable_block_templates', 99, 1 );
How to remove WooCommerce block templates
Picture that you want to use a block theme, but you want to use a custom checkout page.
When WooCommerce is activated, it enables several block templates, and as you know, these HTML files overrides your PHP files.
Below is a list of templates that WooCommerce adds, and an example of how to remove them:
function prefix_disable_block_templates( array $templates ) {
$woocommerce_templates = [
'archive-product',
'single-product',
'cart',
'checkout',
'order-confirmation',
'product-search-results',
'taxonomy-product_tag',
'taxonomy-product_cat',
'taxonomy-product_attribute',
];
foreach ( $templates as $key => $template ) {
foreach ( $woocommerce_templates as $slug ) {
if ( strpos( $template->slug, $slug ) !== false ) {
unset( $templates[ $key ] );
break;
}
}
}
return $templates;
}
add_filter( 'get_block_templates', 'prefix_disable_block_templates', 99, 1 );