Disabling Gutenberg on certain templates

It can be relatively straightforward to make your simple content pages ready for Gutenberg, but complex pages are different.

Consider a modular homepage with complex full-width sections. While you could build many custom blocks to implement this (as I’ve done on my homepage), it might be easier and faster to use the metabox plugin you already know. We often build landing pages using ACF flexible content.

Using a plugin

The Classic Editor plugin is great for disabling Gutenberg site-wide, but is not a good choice for selectively using it on certain pages.

Gutenberg Ramp lets you disable Gutenberg on a per-post-type basis. You could use Gutenberg for posts, but keep the classic editor for pages.

Using a filter

We can use the use_block_editor_for_post_type filter for more fine-grained control. It passes two parameters:

  • $can_edit, boolean, whether Gutenberg should be used to edit
  • $post_type, string

I use $_GET['post'] to get the current Page ID from the URL. If that page is using certain templates, I’ll disable the Gutenberg editor. I also use a similar function to remove the classic editor for current WordPress installs.

I create a disable-editor.php file in my theme and update the $excluded_templates and $excluded_ids arrays specify where I want the editor turned off.

This disables both the Gutenberg Block Editor and Classic Editor, allowing me to use a metabox for constructing the editing experience.

<?php
/**
 * Disable Editor
 *
 * @package      ClientName
 * @author       Bill Erickson
 * @since        1.0.0
 * @license      GPL-2.0+
**/

/**
 * Templates and Page IDs without editor
 *
 */
function ea_disable_editor( $id = false ) {

	$excluded_templates = array(
		'templates/modules.php',
		'templates/contact.php'
	);

	$excluded_ids = array(
		// get_option( 'page_on_front' )
	);

	if( empty( $id ) )
		return false;

	$id = intval( $id );
	$template = get_page_template_slug( $id );

	return in_array( $id, $excluded_ids ) || in_array( $template, $excluded_templates );
}

/**
 * Disable Gutenberg by template
 *
 */
function ea_disable_gutenberg( $can_edit, $post_type ) {

	if( ! ( is_admin() && !empty( $_GET['post'] ) ) )
		return $can_edit;

	if( ea_disable_editor( $_GET['post'] ) )
		$can_edit = false;

	return $can_edit;

}
add_filter( 'gutenberg_can_edit_post_type', 'ea_disable_gutenberg', 10, 2 );
add_filter( 'use_block_editor_for_post_type', 'ea_disable_gutenberg', 10, 2 );

/**
 * Disable Classic Editor by template
 *
 */
function ea_disable_classic_editor() {

	$screen = get_current_screen();
	if( 'page' !== $screen->id || ! isset( $_GET['post']) )
		return;

	if( ea_disable_editor( $_GET['post'] ) ) {
		remove_post_type_support( 'page', 'editor' );
	}

}
add_action( 'admin_head', 'ea_disable_classic_editor' );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Andrew says

    Ahh makes sense. Well thanks for your article! It helped me to realize this was possible and eventually to discover that new hook.