Enabling the Gutenberg (block) editor for a specific post type requires these criteria:
-
The post type must be enabled in the REST API because Gutenberg uses the REST API. And for this, you need to set the
show_in_rest
parameter totrue
when registering the post type — seeregister_post_type()
. -
The post type must support the editor, otherwise, the editor won’t appear on the post editing screen. And for this, you need to add
editor
to thesupports
parameter (which is an array) when registering the post type.
And as you already figured it, you can override (add/edit/remove) a post type’s parameters via the register_post_type_args
hook. But then, you’re overriding the wrong parameter: rewrite
, which doesn’t actually have the show_in_rest
or supports
parameter.
And I already stated the correct parameters that should be overridden/set, so here’s a working example based on your code — note that it’s a better practice to use strict comparison (e.g. ===
for checking equality) and in an expression (or a conditional statement), it’s recommended to put the variable on the right hand side (e.g. 'value' === $my_var
and not $my_var === 'value'
):
function ux_portfolio_block_editor( $args, $post_type ) {
if ( 'featured_item' === $post_type ) {
// 1. Enable in the REST API.
$args['show_in_rest'] = true;
// 2. Add the editor support.
if ( empty( $args['supports'] ) || ! is_array( $args['supports'] ) ) {
$args['supports'] = [ 'editor' ];
} elseif ( ! in_array( 'editor', $args['supports'] ) ) {
$args['supports'][] = 'editor';
}
}
return $args;
}
How about (custom) taxonomies?
Well, it’s similar to post types, except that:
-
Use the
register_taxonomy_args
hook for overriding the taxonomy’s parameters. -
Taxonomies do not use the
supports
parameter, so to enable the taxonomy (selector) in the block editor (on the post editing screen), you’d only need to set theshow_in_rest
parameter totrue
when registering the taxonomy (seeregister_taxonomy()
) or when overriding its parameters.
So here’s an example for a taxonomy having the slug block_cat
:
function ux_portfolio_block_cat_editor( $args, $taxonomy ) {
if ( 'block_cat' === $taxonomy ) {
$args['show_in_rest'] = true;
}
return $args;
}
add_filter( 'register_taxonomy_args', 'ux_portfolio_block_cat_editor', 20, 2 );