editor - Enable Gutenberg on custom post type

admin2025-06-02  2

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?

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?

Share Improve this question asked Dec 30, 2018 at 12:31 ehsanehsan 4531 gold badge4 silver badges8 bronze badges 1
  • this article may help you - webomnizz/… – jogesh_pi Commented Sep 7, 2020 at 7:26
Add a comment  | 

2 Answers 2

Reset to default 72

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.

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 cw_post_type() {

    register_post_type( 'portfolio',
        // WordPress CPT Options Start
        array(
            'labels' => array(
                'name' => __( 'Portfolio' ),
                'singular_name' => __( 'Portfolio' )
            ),
            'has_archive' => true,
            'public' => true,
            'rewrite' => array('slug' => 'portfolio'),
            'show_in_rest' => true,
            'supports' => array('editor')
        )
    );
}

add_action( 'init', 'cw_post_type' );

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.

All the steps and query discuss in detailed at https://www.cloudways/blog/gutenberg-wordpress-custom-post-type/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748811604a313922.html

最新回复(0)