Enable Gutenberg on custom post type

I have this custom post type:

function create_posttype() {
  register_post_type( 'companies',
    array(
      'labels' => array(
        'name' => __( 'شرکتهای عضو' ),
        'singular_name' => __( 'شرکت' )
      ),
      'supports' => array('title', 'editor', 'custom-fields', 'excerpt', 'thumbnail'),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'companies'),
    )
  );
}
add_action( 'init', 'create_posttype' );

Which shows classic editor in WordPress admin area. I tried to replace ‘editor’ with ‘gutenberg’ in the supports array which doesn’t work.
I also added this code to my function as suggested here:

add_filter('gutenberg_can_edit_post_type', 'prefix_disable_gutenberg');
function prefix_disable_gutenberg($current_status, $post_type)
{
    if ($post_type === 'companies') return true;
    return $current_status;
}

How can I have a Gutenberg editor on my custom post type?

2

For Gutenberg to work in a Custom Post Type you need to enable both the editor in supports (which you already have) and show_in_rest. So add 'show_in_rest' => true, to your post registration arguments array.

Leave a Comment