Skip to content

[create-block] Update templates to use the block meta collection changes coming in 6.7 #66484

@ryanwelcher

Description

@ryanwelcher

What problem does this address?

When 6.7 is released, block developers will have access to wp_register_block_metadata_collection(). We should update the default templates to include a call to that function and add build-blocks-manifest to the generated `package.json. This will help to drive adoption of the new performance tools available.

See the full dev note:

https://make.wordpress.org/core/2024/10/17/new-block-type-registration-apis-to-improve-performance-in-wordpress-6-7/

What is your proposed solution?

Add a variation of the following to $slug.php:

/**
 * Registers the block using the metadata loaded from the `block.json` file.
 * Behind the scenes, it registers also all assets so they can be enqueued
 * through the block editor in the corresponding context.
 *
 * @see https://developer.wordpress.org/reference/functions/register_block_type/
 */
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {

        wp_register_block_metadata_collection(
        	__DIR__  . '/build',
        	__DIR__  . '/build/blocks-manifest.php'
        );

	register_block_type( __DIR__ . '/build' );
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );

Update package.json to add the new command and also run it as a post command of build:

"scripts": {
      "build": "wp-scripts build",
      "postbuild": "npm run build-blocks-manifest",
      "build-blocks-manifest": "wp-scripts build-blocks-manifest",
},

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions