Table of Contents
Gutenberg Editor
Register Gutenberg WordPress CPT
Add Gutenberg Support to WordPress CPT
The Complete Code
The Latest “Super” Tool in WordPress: Gutenberg Editor
With the current version of WordPress ( 5.1 RC), Gutenberg is only visible for the default pages and post types. Since WordPress custom post types are almost everywhere, the unavailability of Gutenberg editor is something that the community has been talking about since the release of WordPress 5.0.
This means that if you have CPT on your WordPress 5.x website, you will see the dear old Classic Editor when you create or edit a CPT. While there are plans to take care of this peculiar state of affairs in the upcoming versions, you don’t have to wait for the WordPress core maintenance team to release that update.
In fact, if you wish to use Gutenberg editor with the current CPT on your website, all you have to do is read on!
I will begin by explaining how to register a Gutenberg WordPress custom post type. Next, I will show you how to enable Gutenberg for WordPress custom post types.
Register Gutenberg WordPress Custom Post Type
Start by registering a Gutenberg WordPress custom type. The process is fairly easy and involves adding the following the code snippet.
/*Register WordPress Gutenberg CPT */
function custom_post_type() {
register_post_type( 'book',
// WordPress CPT Options Start
array(
'labels' => array(
'name' => __( 'Book' ),
'singular_name' => __( 'Book' )
),
'has_archive' => true,
'public' => true,
'rewrite' => array('slug' => 'Book'),
)
);
}
add_action( 'init', 'custom_post_type' );
Once the snippet in place, the custom post type has been registered. However, in a surprising twist, when you try to create or edit a custom post type, you will still see the old Classic editor.
Add Gutenberg Support to WordPress Custom Post Types
add support for the editor.
Now, in order to make the Gutenberg editor visible on WordPress custom posts, you need to carry out an additional easy step of adding the following code snippet to the code snippet in the previous section:
add the show_in_rest key and set it to true via your custom post type.
'show_in_rest' => true,
'supports' => array('editor')
As you can see, the above code snippet just set the ‘show_in_rest’ parameter to ‘TRUE’. After this step, when you create or edit a custom post type, you will see the Gutenberg editor visible and enabled.
The Complete Code
Here is the complete code which you put in functions.php, located in the theme folder:
/*Register WordPress Gutenberg CPT */
function custom_post_type() {
register_post_type( 'book',
// WordPress CPT Options Start
array(
'labels' => array(
'name' => __( 'Book' ),
'singular_name' => __( 'Book' )
),
'has_archive' => true,
'public' => true,
'rewrite' => array('slug' => 'Book'),
'show_in_rest' => true,
'supports' => array('editor')
)
);
}
add_action( 'init', 'custom_post_type' );