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 a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

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

Let's Talk

Reader Interactions

Comments

  1. cathy tibbles says

    thank you SO much for this! ACF Pro ‘remove fields’ section doesn’t work any longer with gutenberg. this will do the trick!

    • Bill Erickson says

      The simplest way to limit Gutenberg by post type is using the Gutenberg Ramp plugin.

      The approach outlined above is for a more fine grained approach – limiting it based on the page template within a single post type. While you could use the above code to limit it by post type, Gutenberg will be used until the post is saved/published and a post ID is set, which isn’t optimal.

  2. James says

    Hey Bill, just sharing a snippet that others may find useful here.

    To disable on posts and/or page sitewide this works a treat.

    // Disable for posts
    add_filter(‘use_block_editor_for_post’, ‘__return_false’, 10);

    // Disable for post types
    add_filter(‘use_block_editor_for_post_type’, ‘__return_false’, 10);

  3. Bobby says

    Guys, Can y give a quick guide how to use the file above pls?
    Step 1, step 2 …

    I want to turn off the Classic and Gutenberg editors for a page template that uses an ACF form.
    Thanks!

Leave A Reply