Disabling Gutenberg on certain templates

Update (November 8, 2018)

The previous version of this post used gutenberg_can_edit_post_type. WordPress 5.0 has renamed this to use_block_editor_for_post_type so the code-name “Gutenberg” isn’t used in core.  I’ve updated the post below accordingly.


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

Consider a modular homepage that’s built completely with metaboxes. While theoretically these could be built with Gutenberg in the future, you need to build the template now with the technology you’re familiar with.

Transitioning these pages to Gutenberg will likely require rebuilding them in the future, and you want to ensure they don’t break in the meantime once WordPress 5.0 is released.

Using a plugin

The Classic Editor plugin is a good place to start. It gives you two options:

  1. Use the Gutenberg editor by default, and add an additional “Edit (Classic)” link (screenshot). You have to hope the client uses the correct editor for your custom pages.
  2. Replace Gutenberg site-wide with the old TinyMCE editor. This is less confusing for the client, but they never get to use the new editor anywhere. I don’t think this is a good long-term option, but if you’d like to use it, go to Settings > Writing and uncheck “Do not replace the editor”.

You can use the Gutenberg Ramp plugin to 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.

<?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' );
view raw disable-editor.php hosted with ❤ by GitHub

gutenberg

Receive New Posts by Email

Comments

  1. Do you think that Gutemberg can one day make popular page builders such as VC, elementor and other obsolete? I’m really curious about it

    1. I believe Gutenberg will definitely have an effect on page builders. Many people turned to page builders because they were disappointed with the flexibility of TinyMCE, which Gutenberg is trying to fix.

      But I think there will always be a role to play for page builders on top of (or instead of) Gutenberg. WordPress core won’t go as far with Gutenberg as the page builders have.

  2. Hi. If i install your ‘Disable Gutenberg’ plugin BEFORE WordPress 5.0 rolls out, will it disable Gutenberg when v5.0 is installed?

    1. Yes it will. You can install it now, and once WP 5.0 is out it will automatically disable Gutenberg.

      You can also install the Classic Editor plugin now. They’ve fixed the issues I described above.

  3. Thank you very much for this post, it works like a charm!

    I’m using ACF (custom fields) for some of my templates and they mix better with the old editor’s page. Plus, I’ll be avoiding questions like “Why don’t we just use the block ‘Gallery’ from the plus in the top left corner of the window?”. 😀

    I would love to buy you a coffee when I get my honorarium paid.

    1. No, I haven’t used that plugin before. I recommend sticking with the Classic Editor plugin since it’s the recommendation from WP core and will stay up-to-date with new Gutenberg changes.

Leave a comment