wp_register_block_types_from_metadata_collection() │ WP 6.8.0

Registers all block types from a metadata collection.

The function reads the list of blocks from a PHP manifest and registers each one with register_block_type_from_metadata().

Using a shared manifest speeds up the registration of several blocks. WordPress does not need to read and parse every block.json file separately.

You can also register the collection with this function by passing the manifest path. If the collection is already registered with wp_register_block_metadata_collection(), you can omit the $manifest parameter.

The function does not allow passing individual registration arguments for each block. For example, if you need to specify a custom render_callback, register the collection with wp_register_block_metadata_collection() and the blocks separately with register_block_type().

No Hooks.

Returns

null.

  • null — Does not return anything. The results of individual block registrations are not passed to the calling code.

Usage

wp_register_block_types_from_metadata_collection( $path, $manifest );
$path(string) (required)

Absolute base path to the collection's block directory. It must contain block directories whose names match the manifest keys.

If $manifest is not passed, a metadata collection must already be registered for this path.

$manifest(string)

Absolute path to the PHP manifest file. If specified, the function registers the metadata collection first.

The manifest must return an array in the format block_identifier => metadata. The identifier is specified without a namespace and must match the block directory name.

Default: ''

Examples

#1 Registering all blocks from a manifest

On the init action, register all blocks whose metadata is collected in build/blocks-manifest.php:

The wp-scripts build-blocks-manifest command collects metadata from the build directory and creates the build/blocks-manifest.php file.

add_action( 'init', 'my_plugin_register_blocks' );

function my_plugin_register_blocks() {
	wp_register_block_types_from_metadata_collection(
		__DIR__ . '/build',
		__DIR__ . '/build/blocks-manifest.php'
	);
}

For example, with two blocks the build directory can look like this:

build/
├── blocks-manifest.php
├── button/
│   └── block.json
└── notice/
	└── block.json

#2 Registering blocks from a previously registered collection

If the collection is already registered, you can omit the $manifest parameter:

add_action( 'init', 'my_plugin_register_blocks' );

function my_plugin_register_blocks() {
	$build_path = __DIR__ . '/build';

	wp_register_block_metadata_collection(
		$build_path,
		$build_path . '/blocks-manifest.php'
	);

	wp_register_block_types_from_metadata_collection( $build_path );
}

Notes

See: wp_register_block_metadata_collection()
See: register_block_type_from_metadata()

Changelog

Since 6.8.0 Introduced.

wp_register_block_types_from_metadata_collection() code WP 7.1.4

function wp_register_block_types_from_metadata_collection( $path, $manifest = '' ) {
	if ( $manifest ) {
		wp_register_block_metadata_collection( $path, $manifest );
	}

	$block_metadata_files = WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path );
	foreach ( $block_metadata_files as $block_metadata_file ) {
		register_block_type_from_metadata( $block_metadata_file );
	}
}