register_block_type_from_metadata( string $file_or_folder, array $args = array() ): WP_Block_Type|false
- Since
- 5.5.0, 5.7.0, 5.9.0, 6.1.0, 6.3.0, 6.4.0, 6.5.0, 6.7.0
- Source
wp-includes/blocks.php:520
block.json file.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
$file_or_folderstring- Path to the JSON file with metadata definition for the block or path to the folder where the
block.jsonfile is located.
If providing the path to a JSON file, the filename must end withblock.json. $argsarrayoptional- Array of block type arguments. Accepts any public property of
WP_Block_Type. See WP_Block_Type::__construct() for information on accepted arguments. Default empty array.Default:array()
Return value
WP_Block_Type|false- The registered block type on success, or false on failure.
Performance profile
How much work a call to register_block_type_from_metadata() 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
- 4
- Plugin surface
- 2 hooks
- Called by
- 50
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 390.
Third-party callbacks on 'block_type_metadata', 'block_type_metadata_settings' run inside this call, and their cost is not bounded by anything here.
50 places 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 register_block_type_from_metadata() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 389 | 4 | 56 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 390 | 4 | 56 | |
| 8.4 | 390 | 4 | 56 | 14 fewer instructions than PHP 8.3 |
| 8.3 | 404 | 4 | 56 | |
| 8.2 | 404 | 4 | 56 | 3 more instructions than PHP 8.1 |
| 8.1 | 401 | 4 | 56 | 6 fewer instructions than PHP 7.4 |
| 7.4 | 407 | 4 | 56 |
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 · 2
2 hooks fire while register_block_type_from_metadata() runs, in this order:
- apply_filters( block_type_metadata )filterline 559 (+39 into the body)
Filters the metadata provided for registering a block type.
- apply_filters( block_type_metadata_settings )filterline 814 (+294 into the body)
Filters the settings determined from the block type metadata.
Uses · 19
- wp_normalize_path()Normalizes a filesystem path.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- trailingslashit()Appends a trailing slash.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_json_file_decode()Reads and decodes a JSON file.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- current_theme_supports()Checks a theme's support for a given feature.
- wp_should_load_separate_core_block_assets()Checks whether separate styles should be loaded for core blocks.
- get_block_metadata_i18n_schema()Gets i18n schema for block's metadata read from `block.json` file.
- translate_settings_using_i18n_schema()Translates the provided settings value using its i18n schema.
- remove_block_asset_path_prefix()Removes the block asset's path prefix if provided.
- register_block_script_handle()Finds a script handle for the selected block metadata field.
Show all 19
- register_block_script_module_id()Finds a script module ID for the selected block metadata field.
- register_block_style_handle()Finds a style handle for the block metadata field.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- WP_Block_Metadata_Registry::get_metadata()Retrieves block metadata for a given block within a specific collection.
- WP_Block_Type_Registry::get_instance()::register()
- WP_Block_Type_Registry::get_instance()Utility method to retrieve the main instance of the class.
Used by · 50
- register_block_core_accordion()Registers the `core/accordion` block on server.
- register_block_core_accordion_item()Registers the `core/accordion-item` block on server.
- register_block_core_archives()Register archives block.
- register_block_core_avatar()Registers the `core/avatar` block on the server.
- register_block_core_block()Registers the `core/block` block.
- register_block_core_breadcrumbs()Registers the `core/breadcrumbs` block on the server.
- register_block_core_button()Registers the `core/button` block on server.
- register_block_core_calendar()Registers the `core/calendar` block on server.
- register_block_core_categories()Registers the `core/categories` block on server.
- register_block_core_comment_author_name()Registers the `core/comment-author-name` block on the server.
- register_block_core_comment_content()Registers the `core/comment-content` block on the server.
- register_block_core_comment_date()Registers the `core/comment-date` block on the server.
Show all 50
- register_block_core_comment_edit_link()Registers the `core/comment-edit-link` block on the server.
- register_block_core_comment_reply_link()Registers the `core/comment-reply-link` block on the server.
- register_block_core_comment_template()Registers the `core/comment-template` block on the server.
- register_block_core_comments()Registers the `core/comments` block on the server.
- register_block_core_comments_pagination()Registers the `core/comments-pagination` block on the server.
- register_block_core_comments_pagination_next()Registers the `core/comments-pagination-next` block on the server.
- register_block_core_comments_pagination_numbers()Registers the `core/comments-pagination-numbers` block on the server.
- register_block_core_comments_pagination_previous()Registers the `core/comments-pagination-previous` block on the server.
- register_block_core_comments_title()Registers the `core/comments-title` block on the server.
- register_block_core_cover()Registers the `core/cover` block renderer on server.
- register_block_core_details()Registers the `core/details` block on server.
- register_block_core_file()Registers the `core/file` block on server.
- register_block_core_footnotes()Registers the `core/footnotes` block on the server.
- register_block_core_gallery()Registers the `core/gallery` block on server.
- register_block_core_heading()Registers the `core/heading` block on server.
- register_block_core_home_link()Register the home block
- register_block_core_icon()Registers the `core/icon` block on server.
- register_block_core_image()Registers the `core/image` block on server.
- register_block_core_latest_comments()Registers the `core/latest-comments` block.
- register_block_core_latest_posts()Registers the `core/latest-posts` block on server.
- register_block_core_legacy_widget()Registers the 'core/legacy-widget' block.
- register_block_core_list()Registers the `core/list` block on server.
- register_block_core_loginout()Registers the `core/loginout` block on server.
- register_block_core_media_text()Registers the `core/media-text` block renderer on server.
- register_block_core_navigation()Register the navigation block.
- register_block_core_navigation_link()Registers the navigation link block.
- register_block_core_navigation_overlay_close()Registers the navigation overlay close block.
- register_block_core_navigation_submenu()Register the navigation submenu block.
- register_block_core_page_list()Registers the `core/pages` block on server.
- register_block_core_page_list_item()Registers the `core/page-list-item` block on server.
- register_block_core_paragraph()Registers the `core/paragraph` block on server.
- register_block_core_pattern()Registers the `core/pattern` block on the server.
- register_block_core_playlist()Registers the `core/playlist` block on server.
- register_block_core_playlist_track()Registers the `core/playlist-track` block on server.
- register_block_core_post_author()Registers the `core/post-author` block on the server.
- register_block_core_post_author_biography()Registers the `core/post-author-biography` block on the server.
- register_block_core_post_author_name()Registers the `core/post-author-name` block on the server.
- register_block_core_post_comments_count()Registers the `core/post-comments-count` block on the server.
Source code
function register_block_type_from_metadata( $file_or_folder, $args = array() ) { /* * Get an array of metadata from a PHP file. * This improves performance for core blocks as it's only necessary to read a single PHP file * instead of reading a JSON file per-block, and then decoding from JSON to PHP. * Using a static variable ensures that the metadata is only read once per request. */ $file_or_folder = wp_normalize_path( $file_or_folder ); $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? trailingslashit( $file_or_folder ) . 'block.json' : $file_or_folder; $is_core_block = str_starts_with( $file_or_folder, wp_normalize_path( ABSPATH . WPINC ) ); $metadata_file_exists = $is_core_block || file_exists( $metadata_file ); $registry_metadata = WP_Block_Metadata_Registry::get_metadata( $file_or_folder ); if ( $registry_metadata ) { $metadata = $registry_metadata; } elseif ( $metadata_file_exists ) { $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); } else { $metadata = array(); } if ( ! is_array( $metadata ) || ( empty( $metadata['name'] ) && empty( $args['name'] ) ) ) { return false; } $metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null; /** * Filters the metadata provided for registering a block type. * * @since 5.7.0 * * @param array $metadata Metadata for registering a block type. */ $metadata = apply_filters( 'block_type_metadata', $metadata ); // Add `style` and `editor_style` for core blocks if missing. if ( ! empty( $metadata['name'] ) && str_starts_with( $metadata['name'], 'core/' ) ) { $block_name = str_replace( 'core/', '', $metadata['name'] ); if ( ! isset( $metadata['style'] ) ) { $metadata['style'] = "wp-block-$block_name"; } if ( current_theme_supports( 'wp-block-styles' ) && wp_should_load_separate_core_block_assets() ) { $metadata['style'] = (array) $metadata['style']; $metadata['style'][] = "wp-block-{$block_name}-theme"; } if ( ! isset( $metadata['editorStyle'] ) ) { $metadata['editorStyle'] = "wp-block-{$block_name}-editor"; } } $settings = array(); $property_mappings = array( 'apiVersion' => 'api_version', 'name' => 'name', 'title' => 'title', 'category' => 'category', 'parent' => 'parent', 'ancestor' => 'ancestor', 'icon' => 'icon', 'description' => 'description', 'keywords' => 'keywords', 'attributes' => 'attributes', 'providesContext' => 'provides_context', 'usesContext' => 'uses_context', 'selectors' => 'selectors', 'supports' => 'supports', 'styles' => 'styles', 'variations' => 'variations', 'example' => 'example', 'allowedBlocks' => 'allowed_blocks', ); $textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; $i18n_schema = get_block_metadata_i18n_schema();Changelog
Introduced in 5.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
variations argument.from the docblockallowedBlocks, viewScriptModule, and viewStyle fields.from the docblockblockHooks field.from the docblockselectors field.from the docblockrender field.from the docblockvariations and viewScript fields.from the docblocktextdomain field and i18n handling for all translatable fields.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/blocks.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.