Enabling the block editor for custom post types correctly

  |   By  |  0 Comments

While updating a WordPress plugin recently I wanted to enable the block editor for a custom post type. Strangely, it is not automatic.
After a quick Google, I discovered I needed to add

‘show_in_rest’=>true and ‘supports’=>array(‘editor’)

to the arguments for the register_post_type function. All well and good, but no title field appears at the top of the page which was a disaster.
So here’s how to do it properly

/*Register WordPress Gutenberg CPT */
function my_custom_post_type() {
register_post_type( 'my_custom_post_type',
array(
'labels' => array(
'name' => __( 'My custom posts' ),
'singular_name' => __( 'My custom post' )
),
'has_archive' => true,
'public' => true,
'rewrite' => array('slug' => 'my-custom-posts'),
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'permalinks', 'featured_image' )
)
);
}
add_action( 'init', 'my_custom_post_type' );

Now you get all the features you expect from a normal post on your editor screen.