Recently I had the requirement in a project where I wanted to remove the block editor, only for pages that had a specific page template assigned.
In my case the page template was inside the templates
folder of the theme and the filename was page-content.php
. This meant the page template had a slug of:
templates/page-content.php
The following code, in my case added to the themes functions.php
file meant the editor did not show on pages with that template assigned.
function hd_disable_block_editor_for_content( $use_block_editor, $post ) {
// get the page template slug for this post.
$template = get_page_template_slug( $post->ID );
// if the template slug matches the content template.
if ( 'templates/page-content.php' === $template ) {
// disable the block editor.
$use_block_editor = false;
}
return $use_block_editor;
}
add_filter( 'use_block_editor_for_post', 'hd_disable_block_editor_for_content', 10, 2 );