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. 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.

      • Fabio Nodari says

        I see. Thanks for your reply. I’d rather do things adding code to the function file than installing plugins. Is it possible in many case?

  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!

  4. Chris says

    I got here expecting a completely different result (meaning: your post headline is ‘misleading’ so to speak).

    For example: I can have one post type but use different templates for the same post type. For example I can have pages and choose template under page attributes.

    The big question is how do you disable Gutenberg IF you are using a particular template (which is what your headline suggests). Instead you explain how to disable Gutenberg for a post type which has nothing to do with templates. You can have different post types using the same template.

    Granted, the disabling of Gutenberg should work from the ACF plugin itself. I am dumbfounded by the fact that this has not yet been resolved by them.

    • Bill Erickson says

      I’m sorry but I think you misread the post, or didn’t read it all the way through.

      I describe how to disable Gutenberg from the most broad to the most specific:
      – Site-wide, use Classic Editor
      – Specific post types, use Gutenberg Ramp
      – Specific templates within a post type, use the code I provided.

      Copy the code provided in the post, and change the $excluded_templates array to list the page templates you have in your theme.

      • Chris says

        Yes, I misread the first portion of your post, you are correct. If I can suggest a clearer intro to the article :). Excellent work by they way. Which makes me even more surprised that they still didn’t solve this out of the box.

  5. Andrew says

    Is there any particular reason you couldn’t use `use_block_editor_for_post` instead? This passes `$post` rather than `$post_type`, so there’s no need to use `$_GET` which is less secure in this instance.

    • Bill Erickson says

      You’re right, that’s probably a more appropriate filter. When I first wrote this functionality (before WP 5.0) I don’t believe that filter was in the Gutenberg plugin.

      When Gutenberg was merged into core, I updated the filter name from gutenberg_can_edit_post_type to use_block_editor_for_post_type but didn’t dig in to see if there’s a more appropriate filter.