wordpress How to Disable Gutenberg Complete Editor

0
34

Disable Gutenberg Editor Completely

Update! New way to disable Gutenberg

As expected, Gutenberg Editor is changing constantly. As of now, you can use either of the following filter hooks to disable Gutenberg Editor completely on your entire site:

// 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);

Again, either of these hooks will suffice; no need to add both (although technically it won’t hurt anything). The idea is that the different hooks enable you to identify both single posts and archive views. These hooks work for Gutenberg plugin version 4.1 or later, and WordPress 5.0 (beta) and later. For older versions, use the code below.

Older versions of WP/Gutenberg Editor

To completely disable Gutenberg Editor on older versions, add either of the following filter hooks via functions.php or custom plugin (or even a must-use plugin!):

// disable for posts
add_filter('gutenberg_can_edit_post', '__return_false', 10);

// disable for post types
add_filter('gutenberg_can_edit_post_type', '__return_false', 10);

Again, no need to add both of these lines, either will work to disable Gutenberg Editor everywhere on your site (i.e., for all posts and post types). These filters are the recommended way of disabling Gutenberg. They work with Gutenberg plugin version less than 4.1, and WordPress versions less than 5.0 beta.

Conditional technique

In case it is useful for anyone, here is an example of how to conditionally disable Gutenberg/Block Editor depending on which version of WordPress is running.

// Disable Gutenberg

if (version_compare($GLOBALS['wp_version'], '5.0-beta', '>')) {
	
	// WP > 5 beta
	add_filter('use_block_editor_for_post_type', '__return_false', 10);
	
} else {
	
	// WP < 5 beta
	add_filter('gutenberg_can_edit_post_type', '__return_false', 10);
	
}

Here we use version_compare() to see if WordPress is running at 5.0 (beta) or better. If so, then we disable Gutenberg using the new filter. Otherwise, WordPress is running older version so we disable using the latest filter.

Disable Gutenberg for Custom Post Types

Update! New way to disable Gutenberg

For newer versions of Gutenberg (4.1+), and WordPress 5.0 beta, here is how to disable Gutenberg for specific post types:

function digwp_disable_gutenberg($is_enabled, $post_type) {
	
	if ($post_type === 'book') return false; // change book to your post type
	
	return $is_enabled;
	
}
add_filter('use_block_editor_for_post_type', 'digwp_disable_gutenberg', 10, 2);

This is the same as the older method, the only thing that has changed is the hook. Compare with the code used for older versions..

Older versions of WP/Gutenberg

Here is how to disable Gutenberg on specific post types for older versions of Gutenberg (less than 4.1), and WordPress versions less than 5.0 beta.

The Gutenberg editor is active only on WP posts where it is enabled. By default, this includes WP Posts and Pages. Other post types can be disabled using the same filter, gutenberg_can_edit_post_type. For example:

function digwp_disable_gutenberg($is_enabled, $post_type) {
	
	if ($post_type === 'book') return false; // change book to your post type
	
	return $is_enabled;
	
}
add_filter('gutenberg_can_edit_post_type', 'digwp_disable_gutenberg', 10, 2);

Here we are disabling Gutenberg only for the book post type. So it will only affect books and no other post types. To disable for a different post type, replace book with the name of your post type.

Disable when registering post types

It’s also possible to disable G7G when registering your Custom Post Types. This method only makes sense for CPTs that do not make use of the post editor, as it basically disables the editor panel entirely. To do so, simply omit editor from the supports parameter when registering your post type. Here is an example:

$args = array(
	'label'    => __('Books'),
	'labels'   => $labels,
	'supports' => array(
		'author',
		'custom-fields',
		// 'editor', // <-- do not add this param
		'title',
		'thumbnail'
	),
	'has_archive' => false,
	'hierarchical' => false
);
register_post_type('books', $args);

Because we excluded the editor parameter, the books post type will not include the post editor, and thus will not include any Gutenberg stuff.

Disable when registering post types (via REST API)

Another way to disable Gutenberg when registering custom post types is to disable REST for your post type. This can be done by setting the show_in_restparameter to false, for example:

$args = array(
	'label'        => __('Books'),
	'labels'       => $labels,
	'show_in_rest' => false, // set to false to disable G7G
	'supports'     => array(
		'author',
		'custom-fields',
		'editor', // works even when editor is supported
		'title',
		'thumbnail'
	),
	'has_archive' => false,
	'hierarchical' => false
);
register_post_type('books', $args);

So even if your post type supports the post editor, it’s still possible to disable Gutenberg by disabling the REST API, because Gutenberg requires the REST API in order to work.