函数文档

wp_register_block_types_from_metadata_collection()

💡 云策文档标注

概述

wp_register_block_types_from_metadata_collection() 函数用于从块元数据集合中注册所有块类型。它支持通过路径引用已注册的集合或直接提供清单文件路径来注册集合。

关键要点

  • 函数接受两个参数:$path(必需,集合的绝对基础路径)和 $manifest(可选,清单文件的绝对路径)。
  • 如果提供了 $manifest 参数,会先调用 wp_register_block_metadata_collection() 注册元数据集合。
  • 函数内部使用 WP_Block_Metadata_Registry::get_collection_block_metadata_files() 获取集合中的所有块元数据文件,然后遍历调用 register_block_type_from_metadata() 注册每个块类型。
  • 此函数在 WordPress 6.8.0 版本中引入。

代码示例

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 );
    }
}

📄 原文内容

Registers all block types from a block metadata collection.

Description

This can either reference a previously registered metadata collection or, if the $manifest parameter is provided, register the metadata collection directly within the same function call.

See also

Parameters

$pathstringrequired
The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
$manifeststringoptional
The absolute path to the manifest file containing the metadata collection, in order to register the collection. If this parameter is not provided, the $path parameter must reference a previously registered block metadata collection.

Source

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 );
	}
}

Changelog

Version Description
6.8.0 Introduced.