How to dynamically enable Gutenberg

Whatever your stance is on Gutenberg – it’s coming to WordPress core really soon. There is an easy way to enable/disable the new WordPress editor dynamically using good ol’ filters.

Since Gutenberg version 3.5 there is a filter available called gutenberg_can_edit_post and in WordPress core the same filter is available under the name: use_block_editor_for_post.

Disable by default

In WordPress 5.0 Gutenberg is going to be enabled by default, but I’m going to assume that your default for Gutenberg is to have it turned off. You can turn off Gutenberg by default by either using a plugin like Gutenberg Ramp or with a simple __return_false WordPress filter.

The snippets below are going to assume that $can_edit is false by default, if you want to disable Gutenberg by default, you can do this:

/**
 * Disable Gutenberg by Default
 * but let others easily override that by changing hook priority to 5
 */
add_filter( 'gutenberg_can_edit_post', '__return_false', 5 );
add_filter( 'use_block_editor_for_post', '__return_false', 5 );

This is going to disable Gutenberg by default, but allow other plugins/themes/scripts to easily override it. 

If you want to keep Gutenberg enabled by default, you can do that and just do the opposite of what’s in the functions below.

Enable Gutenberg in a single post

This is one of the simplest examples – disable Gutenberg everywhere (with the snippet above) and then enable Gutenberg in a single post where ID = 1

function maybe_load_gutenberg( $can_edit, $post ) {

	// Make sure the post exists
	if ( empty( $post->ID ) ) {
		return $can_edit;
	}

	// Enable Gutenberg for Post ID 1
	if ( 1 === $post->ID ) {
		return true;
	}

	return $can_edit;
}

// Gutenberg >= 3.5
add_filter( 'gutenberg_can_edit_post', 'maybe_load_gutenberg', 10, 2 );

// WordPress >= 5.0
add_filter( 'use_block_editor_for_post', 'maybe_load_gutenberg', 10, 2 );

Now that Gutenberg is disabled by default, here are a few snippets you could use…

Enable Gutenberg only for new posts

Enabling Gutenberg for new posts consists of 2 parts – first – check if the current screen is the post-new.php page ( for creating a new post ), and then the second bit uses a function called has_blocks() that’s built into Gutenberg and WordPress 5.0 to check if the post content has Gutenberg blocks already in it – this is to make sure that posts written in Gutenberg – stay in Gutenberg.

function maybe_load_gutenberg( $can_edit, $post ) {

	// Make sure the post exists, if not, return whatever $can_edit contains
	if ( empty( $post->ID ) ) {
		return $can_edit;
	}

	// Check for `post-new.php`
	$current = get_current_screen();
	if ( 'post' === $current->base && 'add' === $current->action ) {
		return true;
	}

	// Check whether the content has blocks already in it:
	if ( has_blocks( $post ) ) {
		return true;
	}

	return $can_edit;
}

/**
 * Only enable Gutenberg where we want to
 */
// Gutenberg >= 3.5
add_filter( 'gutenberg_can_edit_post', 'maybe_load_gutenberg', 10, 2 );
// WordPress >= 5.0
add_filter( 'use_block_editor_for_post', 'maybe_load_gutenberg', 10, 2 );

Enable Gutenberg based on Post Meta

You can also load Gutenberg based on post meta data. In this case I’m enabling Gutenberg if the post meta foobar_setting has the value Quick brown fox, but you can get as creative as you need to be with this:

function maybe_load_gutenberg( $can_edit, $post ) {

	// Make sure the post exists
	if ( empty( $post->ID ) ) {
		return $can_edit;
	}

	/**
	 * Disable Gutenberg if post has a meta
	 * with a key `foobar_setting` and value `Quick brown fox`
	 */
	if ( 'Quick brown fox' === get_post_meta( $post->ID, 'foobar_setting', true ) ) {
		return true;
	}

	return $can_edit;
}

/**
 * Only enable Gutenberg where we want to
 */
// Gutenberg >= 3.5
add_filter( 'gutenberg_can_edit_post', 'maybe_load_gutenberg', 10, 2 );
// WordPress >= 5.0
add_filter( 'use_block_editor_for_post', 'maybe_load_gutenberg', 10, 2 );

Enable Gutenberg for a certain category or tag

Putting it all together – you can enable Gutenberg for all new posts and for all posts in a certain category, and have Gutenberg disabled everywhere else. This is just to illustrate that the filter is very flexible:

function maybe_load_gutenberg( $can_edit, $post ) {

	// Make sure the post exists, if not, return whatever $can_edit contains
	if ( empty( $post->ID ) ) {
		return $can_edit;
	}

	// Check for `post-new.php`
	$current = get_current_screen();
	if ( 'post' === $current->base && 'add' === $current->action ) {
		return true;
	}

	// Check whether the content has blocks already in it:
	if ( has_blocks( $post ) ) {
		return true;
	}

	if ( has_category( 'nice-category', $post ) ) {
		return true;
	}

	return $can_edit;
}

// Gutenberg >= 3.5
add_filter( 'gutenberg_can_edit_post', 'maybe_load_gutenberg', 10, 2 );
// WordPress >= 5.0
add_filter( 'use_block_editor_for_post', 'maybe_load_gutenberg', 10, 2 );

Enable or disable Gutenberg by post type

While you could use the snippets above to enable or disable Gutenberg by post type as well, doing something like this:

if ( 'bananas' === get_post_type( $post ) ) {
	return false;
}

I’d recommend to instead use the filter designed for post types: gutenberg_can_edit_post_type

Do this instead:

function maybe_load_gutenberg_for_post_type( $can_edit, $post_type ) {

	if ( 'bananas' === $post_type ) {
		return true;
	}

	return $can_edit;
}

// Gutenberg >= 3.5
add_filter( 'gutenberg_can_edit_post_type', 'maybe_load_gutenberg', 10, 2 );
// WordPress >= 5.0
add_filter( 'use_block_editor_for_post_type', 'maybe_load_gutenberg', 10, 2 );

That’s it!

Sky is the limit here – enable Gutenberg based on the post publish date, allow Gutenberg by tags and so on.

Don’t forget to remove gutenberg_can_edit_post filter eventually after you’ve updated to WordPress 5.0

Leave a Reply

Your email address will not be published. Required fields are marked *