ad

How to Enable Gutenberg On WordPress Custom Post Types

Gutenberg for Custom Post Types

With WordPress 5.0, WordPress also introduced the Gutenberg editor. Gutenberg editor is basically a page builder that lets you add content blocks and have control over the design of each post along with the content. Gutenberg editor has already replaced the TinyMCE content editor in recent WordPress releases pushed after WordPress 5.0.

How To Increase Traffic To Your WordPress Site

Gutenberg editor is available for Posts and Pages but not for Custom Post Types. If your WordPress theme uses Custom Post Types, you will still the classical post editor while editing or create posts on custom post types. WordPress has plans to introduce the Gutenberg editor for custom post types, but you do not need to wait until the official release. If you want, you can enable the Gutenberg editor for WordPress Custom Post Type right now by tweaking the code.

Enable Gutenberg Support to WordPress Custom Post Types

The process to enable the Gutenberg editor on your custom posts needs change in the theme’s functions.php file. If you know how to edit the theme’s code, you can do the changes. Otherwise, take the help of someone who knows PHP and how to modify the WordPress theme code. A wrong change in code will put your site down until you fix the error.

To enable the Gutenberg editor for WordPress custom posts, you need to add two lines of code in the code used to register that custom post type.

'show_in_rest' => true,

You just add the above line in your existing arguments array in the code used to register custom post type.

Here is the default code for a custom Post Type.

function mycpt_posttype() {
register_post_type( 'MyCPT',
    array(
        'labels' => array(
            'name' => __( 'My CPT' ),
            'singular_name' => __( 'CPT' )
        ),
        'public' => true,
                    'show_ui' => true,
                    'show_in_menu'        => true,
                    'show_in_nav_menus'   => true,
                    'show_in_admin_bar'   => true,
        'rewrite' => array('slug' => 'my-cpt'),
                    'supports' => array( 'title', 'editor', 'excerpt', 'comments', 'thumbnail', 'author', 'page-attributes'),

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

To enable the Gutenberg Editor for this Custom Post Type, the code will become as follows

function mycpt_posttype() {
register_post_type( 'MyCPT',
    array(
        'labels' => array(
            'name' => __( 'My CPT' ),
            'singular_name' => __( 'CPT' )
        ),
        'public' => true,
                    'show_ui' => true,
                    'show_in_menu'        => true,
                    'show_in_nav_menus'   => true,
                    'show_in_admin_bar'   => true,
        'show_in_rest' => true,
        'rewrite' => array('slug' => 'my-cpt'),
                    'supports' => array( 'title', 'editor', 'excerpt', 'comments', 'thumbnail', 'author', 'page-attributes'),

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

Wrapping up!

After you have made the change in code, you will now see the Gutenberg editor on your custom posts. You will have to convert old posts to blocks for using the power of the Gutenberg editor on your custom posts.

Tags: | |
Nord VPN Sale

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip Developed By Deepanker