Enable the Block Editor and REST API for a Plugin's Post Type
Turns on REST API support for post types registered without it, which also switches them to the block editor and makes them available to apps and headless sites.
Opens WPIDE on
Code
// Post type => REST base (the URL part after /wp-json/wp/v2/).
$wpide_rest_types = array(
'portfolio' => 'portfolio',
);
add_filter( 'register_post_type_args', function ( $args, $post_type ) use ( $wpide_rest_types ) {
if ( isset( $wpide_rest_types[ $post_type ] ) ) {
$args['show_in_rest'] = true;
$args['rest_base'] = $wpide_rest_types[ $post_type ];
}
return $args;
}, 10, 2 );
Notes
The post type also needs editor support for the block editor to appear. Test the plugin's own meta boxes, which may not all work in the block editor.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.