How to Enable Gutenberg On WordPress Custom Post Types

WordPress

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.

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 creating 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.

Best Managed WordPress Hosting

WPEngine is the best and most secure managed hosting provider

Enable Gutenberg Support to WordPress Custom Post Types

The process to enable the Gutenberg editor on your custom posts needs to 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 the custom post type.

Here is the default code for a custom Post Type.

function twg_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', 'twg_posttype' );

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

function twg_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', 'twg_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 by using the power of the Gutenberg editor on your custom posts. Gutenberg editor makes it easy to customize the look of your blog posts. In classic editor, you could only add text, images, and links. Now you can have a multicolumn layout, add different visual elements like sliders, add galleries, and more. It also makes it easy to enter the tables and manage data.

Tags:

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.


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.

© 2022 The WP Guides Developed By Deepanker