Use Create-Block for a Multi-block Plugin

Nate Finch wrote post on the WordPress Developer blog about how to use Create-Block to make a multi-block plugin. I’m condensing that here so it’s easier for me to remember and find.

First use create block to make a block plugin. Run from within the /wp-content/plugins directory:

npx @wordpress/create-block@latest name-of-plugin

Then delete the contents of /name-of-plugin/src

Then cd into /name-of-plugin/src and create the various blocks

npx wordpress/create-block@latest first-block –no-plugin
npx wordpress/create-block@latest second-block –no-plugin –variant dynamic

Note the -no-plugin option.

Then update the plugin’s main PHP file to register the blocks

function create_block_name_of_plugin_block_init() {
	register_block_type( __DIR__ . '/build/first-block' );
	register_block_type( __DIR__ . '/build/second-block' );
}
add_action( 'init', 'create_block_name_of_plugin_block_init' );

Now you can build the block (npm start) and activate the plugin.