Block Editor PHP Beginner

Use the Block Editor for a Custom Post Type

Switch a plugin's custom post type that opens in the classic editor over to the block editor.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 9 lines
add_filter( 'register_post_type_args', function ( array $args, string $post_type ): array {
	if ( 'portfolio' === $post_type ) {
		$args['show_in_rest'] = true;

		// The block editor also needs the editor feature.
		$args['supports'] = array_unique( array_merge( (array) ( $args['supports'] ?? [ 'title' ] ), [ 'editor' ] ) );
	}
	return $args;
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

The block editor only works for post types that are available in the REST API. If a plugin or theme registers a post type without that, it opens in the classic editor. This snippet turns the REST API on for it.

How it works

The register_post_type_args filter changes a post type’s settings as it’s registered. Setting show_in_rest to true makes it available to the REST API, and adding editor to its supports gives it a content area. WordPress then opens it in the block editor.

Things to watch

  • Some plugins hide their REST API on purpose. Check the post type still works in the plugin’s own screens.
  • Turning on the REST API makes published entries readable at /wp-json/wp/v2/, like posts.
  • Change 'portfolio' to the post type’s name.

How to test it

Edit an entry of that post type. It should open in the block editor.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only