WP_Block_Metadata_Registry::register_collection( string $path, string $manifest ): bool
- Since
- 6.7.0
- Source
wp-includes/class-wp-block-metadata-registry.php:84
Description
This method allows registering a collection of block metadata from a single manifest file, improving performance for large sets of blocks.
The manifest file should be a PHP file that returns an associative array, where the keys are the block identifiers (without their namespace) and the values are the corresponding block metadata arrays. The block identifiers must match the parent directory name for the respective block.json file.
Example manifest file structure:
return array(
'example-block' => array(
'title' => 'Example Block',
'category' => 'widgets',
'icon' => 'smiley',
// ... other block metadata
),
'another-block' => array(
'title' => 'Another Block',
'category' => 'formatting',
'icon' => 'star-filled',
// ... other block metadata
),
// ... more block metadata entries
);Compatibility
- WordPress
- since 6.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$pathstring- The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
$manifeststring- The absolute path to the manifest file containing the metadata collection.
Return value
bool- True if the collection was registered successfully, false otherwise.
Performance profile
How much work a call to WP_Block_Metadata_Registry::register_collection() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 9
- Plugin surface
- 1 hook
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 79.
Third-party callbacks on 'wp_allowed_block_metadata_collection_roots' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Block_Metadata_Registry::register_collection() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9 | wp_normalize_path(), rtrim() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 79 | 9 | 2 | |
| 8.5 | 79 | 9 | 2 | |
| 8.4 | 79 | 9 | 2 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 82 | 9 | 2 | |
| 8.2 | 82 | 9 | 2 | |
| 8.1 | 82 | 9 | 2 | 9 more instructions than PHP 7.4 |
| 7.4 | 73 | 42–54 | 2 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Block_Metadata_Registry::register_collection() runs, in this order:
- apply_filters( wp_allowed_block_metadata_collection_roots )filterline 110 (+26 into the body)
Filters the root directory paths for block metadata collections.
Uses · 8
- wp_normalize_path()Normalizes a filesystem path.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- esc_html()Escaping for HTML blocks.
- wp_get_list_item_separator()Retrieves the list item separator based on the locale.
- WP_Block_Metadata_Registry::get_default_collection_roots()Gets the default collection root directory paths.
- WP_Block_Metadata_Registry::is_valid_collection_path()Checks whether the given block metadata collection path is valid against the list of collection roots.
Used by · 1
- wp_register_block_metadata_collection()Registers a block metadata collection.
Source code
public static function register_collection( $path, $manifest ) { $path = rtrim( wp_normalize_path( $path ), '/' ); $collection_roots = self::get_default_collection_roots(); /** * Filters the root directory paths for block metadata collections. * * Any block metadata collection that is registered must not use any of these paths, or any parent directory * path of them. Most commonly, block metadata collections should reside within one of these paths, though in * some scenarios they may also reside in entirely different directories (e.g. in case of symlinked plugins). * * Example: * * It is allowed to register a collection with path `WP_PLUGIN_DIR . '/my-plugin'`. * * It is not allowed to register a collection with path `WP_PLUGIN_DIR`. * * It is not allowed to register a collection with path `dirname( WP_PLUGIN_DIR )`. * * The default list encompasses the `wp-includes` directory, as well as the root directories for plugins, * must-use plugins, and themes. This filter can be used to expand the list, e.g. to custom directories that * contain symlinked plugins, so that these root directories cannot be used themselves for a block metadata * collection either. * * @since 6.7.2 * * @param string[] $collection_roots List of allowed metadata collection root paths. */ $collection_roots = apply_filters( 'wp_allowed_block_metadata_collection_roots', $collection_roots ); $collection_roots = array_unique( array_map( static function ( $allowed_root ) { return rtrim( wp_normalize_path( $allowed_root ), '/' ); }, $collection_roots ) ); // Check if the path is valid: if ( ! self::is_valid_collection_path( $path, $collection_roots ) ) { _doing_it_wrong( __METHOD__, sprintf( /* translators: %s: list of allowed collection roots */ __( 'Block metadata collections cannot be registered as one of the following directories or their parent directories: %s' ), esc_html( implode( wp_get_list_item_separator(), $collection_roots ) ) ), '6.7.2' ); return false; } if ( ! file_exists( $manifest ) ) { _doing_it_wrong( __METHOD__, __( 'The specified manifest file does not exist.' ), '6.7.0' ); return false; } self::$collections[ $path ] = array( 'manifest' => $manifest, 'metadata' => null, ); return true; }Changelog
Introduced in 6.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-block-metadata-registry.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.