Cyber Week Deals! Save 80% on all Shared Hosting Plans & Locations - Starting from $1.99/mo

How to Selectively Enable Gutenberg Block Editor

Updated on Nov 24, 2022

Many people are aware that they can disable Gutenberg for specific post types, user roles, post IDs, and so on. However, not everyone is aware that the opposite is possible — the conditional enabling of Gutenberg. If Gutenberg is disabled by default, you can enable it on your preferred post types, user roles, and so on.

This guide will show you how to enable Gutenberg using WordPress filter hooks. We'll show you how to enable Gutenberg for individual posts, post meta, new posts, tags, categories, and post types.

This post includes:

Note

This post is primarily for WordPress theme and plugin developers. However, if you are interested, you can find out more about hooks and actions from the official source.

How to Disable Gutenberg by Default

After WordPress 5.0, Gutenberg is enabled by default. As a result, if you want to use it everywhere, it is already there, and you don't need to do anything.

However, if you want Gutenberg to be enabled only on specific post IDs, post types, and so on, you must first disable Gutenberg in general. You can accomplish this by utilizing one of the filter hooks listed below (depending on the WordPress version).

// WP < 5.0 beta
add_filter('gutenberg_can_edit_post', '__return_false', 5);
// WP >= 5.0
add_filter('use_block_editor_for_post', '__return_false', 5);

You can choose either or both of these hooks and add them to your theme’s functions.php file. You can also opt for disabling Gutenberg and restoring the Classic Editor via a plugin — Disable Gutenberg. The plugin is extremely lightweight, flexible, and simple to configure, and it is ideal for sites where Gutenberg should be disabled. Whichever method you use, you must disable Gutenberg for the following techniques to work properly.

Note

In the two filters above, you probably noticed the third parameter (5). Setting that value allows you to override and enable Gutenberg using techniques such as those described below.

How to Enable Gutenberg for Any Post IDs

Once Gutenberg is disabled everywhere, here is an example showing how to enable it only for specific post IDs:

function shapeSpace_enable_gutenberg_post_ids($can_edit, $post) {
if (empty($post->ID)) return $can_edit;
if (1 === $post->ID) return true;
return $can_edit;
}
// Enable Gutenberg for WP < 5.0 beta
add_filter('gutenberg_can_edit_post', 'shapeSpace_enable_gutenberg_post_ids', 10, 2);
// Enable Gutenberg for WordPress >= 5.0
add_filter('use_block_editor_for_post', 'shapeSpace_enable_gutenberg_post_ids', 10, 2);

As written, this function will enable Gutenberg on post ID = 1. You can change that as needed in the third line of the function.

How to Enable Gutenberg for New Posts

To enable Gutenberg for all new posts, you can do something like this:

function shapeSpace_enable_gutenberg_new_posts($can_edit, $post) {
if (empty($post->ID)) return $can_edit;
$current = get_current_screen();
if ('post' === $current->base && 'add' === $current->action) return true;
return $can_edit;
}
// Enable Gutenberg for WP < 5.0 beta
add_filter('gutenberg_can_edit_post', 'shapeSpace_enable_gutenberg_new_posts', 10, 2);
// Enable Gutenberg for WordPress >= 5.0
add_filter('use_block_editor_for_post', 'shapeSpace_enable_gutenberg_new_posts', 10, 2);

This function checks if the user is viewing the post-new.php screen and returns true if so (to enable Gutenberg).

Take note of the 10 values that these techniques pass through the add filter hooks. Why? Remember that when we disable Gutenberg, we set that parameter to 5, so by changing it to 10 or any value greater than 5, we can easily override the disabling function and enable Gutenberg. It all comes down to hook priority.

How to Enable Gutenberg for specific Post Meta

What about enabling Gutenberg Block Editor only on posts that have some specific metadata attached? Easy, here is an example showing how to do it:

function shapeSpace_enable_gutenberg_post_meta($can_edit, $post) {
if (empty($post->ID)) return $can_edit;
if ('Happy' === get_post_meta($post->ID, 'current_mood', true)) return true;
return $can_edit;
}
// Enable Gutenberg for WP < 5.0 beta
add_filter('gutenberg_can_edit_post', 'shapeSpace_enable_gutenberg_post_meta', 10, 2);
// Enable Gutenberg for WordPress >= 5.0
add_filter('use_block_editor_for_post', 'shapeSpace_enable_gutenberg_post_meta', 10, 2);

This function, as written, searches the current post for a custom field named current mood with the value Happy. If it exists, the function returns true, allowing Gutenberg to be enabled for that post. Please keep in mind that these examples are kept as simple as possible to aid comprehension. A lot more is possible!

Note

Notice that we hook the function name into both Gutenberg filter hooks: gutenberg_can_edit_post and use_block_editor_for_post. This means that the function will run in all applicable versions of WordPress. So if you are not worried about supporting older or newer versions of WordPress, you can simply remove one or the other add_filter() functions and be done.

How to Enable Gutenberg for Specific Categories

Here is an example showing how to enable Gutenberg only for specific categories:

function shapeSpace_enable_gutenberg_post_cats($can_edit, $post) {
if (empty($post->ID)) return $can_edit;
if (has_category(12)) return true;
return $can_edit;
}
// Enable Gutenberg for WP < 5.0 beta
add_filter('gutenberg_can_edit_post', 'shapeSpace_enable_gutenberg_post_cats', 10, 2);
// Enable Gutenberg for WordPress >= 5.0
add_filter('use_block_editor_for_post', 'shapeSpace_enable_gutenberg_post_cats', 10, 2);

As written, this function uses WP's has_category() to check if the current post belongs to category 12; if so, true is returned, enabling Gutenberg. Of course, you can specify your own category or array of categories, or whatever.

Note

To check if the current post contains any Gutenberg blocks, we can add this logic to any of our enabling functions: if (has_blocks($post)) return true;

How to Enable Gutenberg for Specific Tags

Here is an example showing how to enable Gutenberg only for specific tags:

function shapeSpace_enable_gutenberg_post_tags($can_edit, $post) {
if (empty($post->ID)) return $can_edit;
if (has_tag(50)) return true;
return $can_edit;
}
// Enable Gutenberg for WP < 5.0 beta
add_filter('gutenberg_can_edit_post', 'shapeSpace_enable_gutenberg_post_tags', 10, 2);
// Enable Gutenberg for WordPress >= 5.0
add_filter('use_block_editor_for_post', 'shapeSpace_enable_gutenberg_post_tags', 10, 2);

As written, this function uses WP's has_tag() to check if the current post is tagged with tag ID = 50; if so, true is returned, enabling Gutenberg. Of course, you can specify your own tag or array of tags.

Tip: Notice the 3rd parameter, 2, passed to either of the add_filter() functions. That specifies the number of parameters passed to the hooked function, which in this case is shapeSpace_enable_gutenberg_post_tags. So if you look at that function, you will see that it accepts two parameters, $can_edit, and $post.

How to Enable Gutenberg for any Post Type

One more for the road! Here is an example showing how to enable Gutenberg only for specific post types:

function shapeSpace_enable_gutenberg_post_type($can_edit, $post) {
if (empty($post->ID)) return $can_edit;
if ('books' === $post_type) return true;
return $can_edit;
}
// Enable Gutenberg for WP < 5.0 beta
add_filter('gutenberg_can_edit_post_type', 'shapeSpace_enable_gutenberg_post_type', 10, 2);
// Enable Gutenberg for WordPress >= 5.0
add_filter('use_block_editor_for_post_type', 'shapeSpace_enable_gutenberg_post_type', 10, 2);

This function is similar to the others, but it does something slightly different in terms of hooks. When it comes to working with post types, WordPress/Gutenberg provides the following filter hooks:

// WP < 5.0 beta
gutenberg_can_edit_post_type
// WP >= 5.0
Use_block_editor_for_post_type

Make sure to include 'show_in_rest' => true, 'supports' => array('editor') when defining your Custom Post Type.

Conclusion

These recommended hooks are used to enable Gutenberg for specific post types. Also, keep in mind that our function is currently checking the current post type. As written, it looks for a post type called books; feel free to change it to suit your needs. The possibilities are endless!

We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!

WordPress Hosting

  • Free WordPress Installation
  • 24/7 WordPress Support
  • Free Domain Transfer
  • Hack-free Protection
  • Fast SSD Storage
  • Free WordPress Transfer
  • Free CloudFlare CDN
  • Immediate Activation
View More