Search

How to Allow Gutenberg Editor for CPT’s

Gutenberg is the new editor that comes by default in WordPress (version 5.0+). However, it turns out that it does not automatically appear for Custom Post Types.

By default, these use the classic WordPress editor. It’s possible (quite likely, actually) that in the future, they will be natively supported in Gutenberg. But for now, to add support for the new editor, a small change is required when creating the new content type.

Here’s an example:

function savvy_cpt_init() {

    $labels = array(
        // not revelant for this article
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'textdomain' ),
        'public'             => true,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'newcpt', $args );

}
add_action( 'init', 'savvy_cpt_init' );

In order to add support for Gutenberg to the custom content type we created, we need to set show_in_rest to true as follows:

function savvy_cpt_init() {

    $labels = array(
        // not revelant for this article
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'textdomain' ),
        'public'             => true,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'show_in_rest'       => true,
    );

    register_post_type( 'newcpt', $args );

}
add_action( 'init', 'savvy_cpt_init' );

In general, enabling the REST API for the custom post type (CPT) you created is necessary for Gutenberg to work with this or any other content type.

Roee Yossef
Roee Yossef

I develop websites & custom WordPress themes by design. I love typography, colors & everything between, and aim to provide high performance, seo optimized websites with a clean & semantic code.

0 Comments...

Leave a Comment

Up!
Blog