wppaste
WordPress

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
Registers a block type from the metadata stored in the 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.json file is located.
If providing the path to a JSON file, the filename must end with block.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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4

Executed per call on PHP 8.5. The body compiles to 390.

Plugin surface
2 hooks

Third-party callbacks on 'block_type_metadata', 'block_type_metadata_settings' run inside this call, and their cost is not bounded by anything here.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always4none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3894561 fewer instruction than PHP 8.5
8.5390456
8.439045614 fewer instructions than PHP 8.3
8.3404456
8.24044563 more instructions than PHP 8.1
8.14014566 fewer instructions than PHP 7.4
7.4407456

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:

  1. apply_filters( block_type_metadata )filterline 559 (+39 into the body)

    Filters the metadata provided for registering a block type.

  2. apply_filters( block_type_metadata_settings )filterline 814 (+294 into the body)

    Filters the settings determined from the block type metadata.

Uses · 19

Show all 19

Used by · 50

Show all 50

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.7.0
Allow PHP filename as variations argument.from the docblock
6.5.0
Added support for allowedBlocks, viewScriptModule, and viewStyle fields.from the docblock
6.4.0
Added support for blockHooks field.from the docblock
6.3.0
Added selectors field.from the docblock
6.1.0
Added support for render field.from the docblock
5.9.0
Added support for variations and viewScript fields.from the docblock
5.7.0
Added support for textdomain field and i18n handling for all translatable fields.from the docblock
5.5.0
Introduced.from the docblock

About 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.