Selective DeGutenberging

The following posts are coming up!

Recent Posts



Okay let’s be honest, friends. Not everything is ready for Gutenberg. In fact, I myself have certain sites that aren’t ready for it today. That’s why there exists an excellent plugin known as the Classic Editor. This allows you to decide it you want to block Gutenberg (which is the default option) or if you want to allow users to chose between Gutenberg and Classic editors.

The settings page for the Classic editor.

But what if you want to go a step further?

What if you want to have Gutenberg on by default for posts and pages, but not for a custom post type?

Don’t worry. We can do that.

The Code

The basic idea is that if WP is 5.0 or higher, make sure that the Classic Editor is installed. If it’s less than 5.0, make sure Gutenberg is installed. Then block the code as needed:

class HELF_Gutenberg {

	public $gutenfree = array();

	public function __construct() {
		$this->gutenfree = array( 'post_type_ONE', 'post_type_TWO', 'post_type_THREE' );
		add_action( 'current_screen', array( $this, 'gutenberg_removal' ) );
	}

	public function gutenberg_removal() {

		// WP 5.0+ requires Classic Editor
		// WP 4.9- requires Gutenberg
		if ( ( version_compare( get_bloginfo( 'version' ), 5.0, '<=' ) && ! is_plugin_active( 'gutenberg/gutenberg.php' ) ) || ( version_compare( get_bloginfo( 'version' ), 5.0, '=>' ) && ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) ) {
			return;
		}

		// Intercept Post Type
		$current_screen    = get_current_screen();
		$current_post_type = $current_screen->post_type;

		// If this is one of our custom post types, we don't gutenize
		if ( in_array( $current_post_type, $this->gutenfree, true ) ) {
			remove_filter( 'replace_editor', 'gutenberg_init' );
			remove_action( 'load-post.php', 'gutenberg_intercept_edit_post' );
			remove_action( 'load-post-new.php', 'gutenberg_intercept_post_new' );
			remove_action( 'admin_init', 'gutenberg_add_edit_link_filters' );
			remove_filter( 'admin_url', 'gutenberg_modify_add_new_button_url' );
			remove_action( 'admin_print_scripts-edit.php', 'gutenberg_replace_default_add_new_button' );
			remove_action( 'admin_enqueue_scripts', 'gutenberg_editor_scripts_and_styles' );
			remove_filter( 'screen_options_show_screen', '__return_false' );
		}
	}

}

new HELF_Gutenberg();

Posted

in

by

Comments

2 responses to “Selective DeGutenberging”

  1. Elizabeth Reiher Avatar
    Elizabeth Reiher

    This a great idea.
    Two questions:
    1) Where would you put this code?
    2) What is meant by “Then block the code as needed.” How do you block the code?

    1. Ipstenu (Mika Epstein) Avatar
      1. I’d put it in a plugin. Since I have a plugin where I’ve created the custom post types already, it sits in there.

      2. Block the code is be being silly, but it just means “Block Gutenberg for your CPTs as necessary.”

%d bloggers like this: