Registers a block metadata collection.
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
);
Parameters
$path
stringrequired- The absolute base path for the collection ( e.g., WP_PLUGIN_DIR .
'/my-plugin/blocks/'
). $manifest
stringrequired- The absolute path to the manifest file containing the metadata collection.
Source
public static function register_collection( $path, $manifest ) {
$path = wp_normalize_path( rtrim( $path, '/' ) );
$wpinc_dir = self::get_wpinc_dir();
$plugin_dir = self::get_plugin_dir();
// Check if the path is valid:
if ( str_starts_with( $path, $plugin_dir ) ) {
// For plugins, ensure the path is within a specific plugin directory and not the base plugin directory.
$relative_path = substr( $path, strlen( $plugin_dir ) + 1 );
$plugin_name = strtok( $relative_path, '/' );
if ( empty( $plugin_name ) || $plugin_name === $relative_path ) {
_doing_it_wrong(
__METHOD__,
__( 'Block metadata collections can only be registered for a specific plugin. The provided path is neither a core path nor a valid plugin path.' ),
'6.7.0'
);
return false;
}
} elseif ( ! str_starts_with( $path, $wpinc_dir ) ) {
// If it's neither a plugin directory path nor within 'wp-includes', the path is invalid.
_doing_it_wrong(
__METHOD__,
__( 'Block metadata collections can only be registered for a specific plugin. The provided path is neither a core path nor a valid plugin path.' ),
'6.7.0'
);
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
Version | Description |
---|---|
6.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.