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

I don’t like how difficult they’ve made this plugin to use. If you want to disable Gutenberg from being added to your live site, you first have to install Gutenberg to access the checkbox to disable Gutenberg!

For my clients who prefer to disable Gutenberg completely, I’m using a new plugin I wrote, Disable Gutenberg. It uses the filter described below but applies it site-wide.

Using a filter

We can use the gutenberg_can_edit_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 );
/**
* 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

Leave a comment