The Gutenberg editor is the new default editor in WordPress and offers a more user-friendly and intuitive editing experience for users. If you have a custom post type on your WordPress site and want to enable Gutenberg on it, this article will guide you through the steps.
Step 1: Check if Your Custom Post Type Supports Gutenberg
Before enabling Gutenberg on your custom post type, you should check if it supports Gutenberg. By default, custom post types should support Gutenberg, but it’s possible that they were created before the Gutenberg editor was introduced. To check if your custom post type supports Gutenberg, add the following code to your functions.php
file:
add_post_type_support( 'your_custom_post_type', 'editor' );
Replace "your_custom_post_type"
with the name of your custom post type. This code adds support for the editor to your custom post type, which should enable Gutenberg.
Step 2: Set show_in_rest
to true
Next, you’ll need to make sure that the show_in_rest
parameter is set to true for your custom post type. You can do this by adding the following code to your functions.php
file:
add_action( 'init', 'wp_daily_enable_rest_api_for_custom_post_type' );
function wp_daily_enable_rest_api_for_custom_post_type() {
global $wp_post_types;
if ( isset( $wp_post_types['your_custom_post_type'] ) ) {
$wp_post_types['your_custom_post_type']->show_in_rest = true;
}
}
Replace your_custom_post_type
with the name of your custom post type. If you are creating a custom post type make sure to have the following options for 'supports'
and show_in_rest
:
function wp_daily_event_post_type() {
$args = array(
'labels' => array(
'name' => _x( 'Events', 'Post type general name', 'event' ),
'singular_name' => _x( 'Event', 'Post type singular name', 'event' ),
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'event' ),
'capability_type' => 'post',
'has_archive' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail' ),
'show_in_rest' => true
);
register_post_type( 'event', $args );
}
add_action('init', 'wp_daily_event_post_type');
Step 3: Enable Gutenberg on Your Custom Post Type
Now that you’ve ensured that your custom post type supports Gutenberg and is exposed in the WordPress REST API, you can enable Gutenberg on your custom post type by adding the following code to your functions.php
file:
add_filter( 'use_block_editor_for_post_type', 'wp_daily_enable_gutenberg_on_custom_post_type', 10, 2 );
function wp_daily_enable_gutenberg_on_custom_post_type( $can_edit, $post_type ) {
if ( 'your_custom_post_type' === $post_type ) {
$can_edit = true;
}
return $can_edit;
}
Again, replace your_custom_post_type
with the name of your custom post type.
Step 4: Test Gutenberg on Your Custom Post Type
Now that you’ve enabled Gutenberg on your custom post type, you can test it by creating or editing a post in your custom post type. You should see the Gutenberg editor instead of the classic editor.
Conclusion
Enabling Gutenberg on a custom post type is a simple process that can greatly enhance your editing experience. By following the steps outlined in this updated article, you can quickly enable Gutenberg on your custom post type and take advantage of its many features.