wppaste
WordPress

register_block_script_handle( array $metadata, string $field_name, int $index = 0 ): string|false

Since
5.5.0, 6.1.0, 6.5.0
Source
wp-includes/blocks.php:267
Finds a script handle for the selected block metadata field.

Description

Detects when a path to a file was provided and optionally finds a corresponding asset file with details necessary to register the script. The handle is taken from the asset file when it provides one, and is otherwise generated automatically. It returns the unprocessed script handle when a handle rather than a path was given.

Compatibility

WordPress
since 6.5.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

$metadataarray
Block metadata.
$field_namestring
Field name to pick from metadata.
$indexintoptional
Index of the script to register when multiple items passed.
Default 0.Default: 0

Return value

string|false
Script handle provided directly or created through script's registration, or false on failure.

Performance profile

How much work a call to register_block_script_handle() 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
6–111

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 117.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost register_block_script_handle() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–12none
!empty($metadata[$field_name]) && $script_handle_or_path === false16–20remove_block_asset_path_prefix()
!empty($metadata[$field_name]) && $script_handle_or_path !== false && !$script_asset && wp_script_is()46–52remove_block_asset_path_prefix(), substr_replace(), realpath(), wp_normalize_path(), wp_script_is()
!empty($metadata[$field_name]) && $script_handle_or_path !== false && $script_asset && wp_script_is()54–60remove_block_asset_path_prefix(), substr_replace(), realpath(), wp_normalize_path(), generate_block_asset_handle(), wp_script_is()
!empty($metadata[$field_name]) && $script_handle_or_path !== false && !$script_asset && !wp_script_is()80–97remove_block_asset_path_prefix(), substr_replace(), realpath(), wp_normalize_path(), wp_script_is(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_script()
!empty($metadata[$field_name]) && $script_handle_or_path !== false && $script_asset && !wp_script_is()88–105remove_block_asset_path_prefix(), substr_replace(), realpath(), wp_normalize_path(), generate_block_asset_handle(), wp_script_is(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_script()
!empty($metadata[$field_name]) && $script_handle_or_path !== false && !$script_asset && !wp_script_is() && !empty($metadata) && $script_dependencies91–103remove_block_asset_path_prefix(), substr_replace(), realpath(), wp_normalize_path(), wp_script_is(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_script(), wp_set_script_translations()
!empty($metadata[$field_name]) && $script_handle_or_path !== false && $script_asset && !wp_script_is() && !empty($metadata) && $script_dependencies99–111remove_block_asset_path_prefix(), substr_replace(), realpath(), wp_normalize_path(), generate_block_asset_handle(), wp_script_is(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_script(), wp_set_script_translations()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1156–109152 fewer instructions than PHP 8.5
8.51176–11115
8.41176–111155 fewer instructions than PHP 8.3
8.31226–11615
8.21226–11615
8.11226–11615
7.41226–11615

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.

Uses · 7

Used by · 1

Source code

function register_block_script_handle( $metadata, $field_name, $index = 0 ) {	if ( empty( $metadata[ $field_name ] ) ) {		return false;	} 	$script_handle_or_path = $metadata[ $field_name ];	if ( is_array( $script_handle_or_path ) ) {		if ( empty( $script_handle_or_path[ $index ] ) ) {			return false;		}		$script_handle_or_path = $script_handle_or_path[ $index ];	} 	$script_path = remove_block_asset_path_prefix( $script_handle_or_path );	if ( $script_handle_or_path === $script_path ) {		return $script_handle_or_path;	} 	$path                  = dirname( $metadata['file'] );	$script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );	$script_asset_path     = wp_normalize_path(		realpath( $script_asset_raw_path )	); 	// Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.	/** @var array{ handle?: non-falsy-string, dependencies?: list<non-falsy-string>, version?: string|false|null, ... } $script_asset */	$script_asset  = ! empty( $script_asset_path ) ? require $script_asset_path : array();	$script_handle = $script_asset['handle'] ??		generate_block_asset_handle( $metadata['name'], $field_name, $index );	if ( wp_script_is( $script_handle, 'registered' ) ) {		return $script_handle;	} 	$script_path_norm    = wp_normalize_path( realpath( $path . '/' . $script_path ) );	$script_uri          = get_block_asset_url( $script_path_norm );	$script_dependencies = $script_asset['dependencies'] ?? array();	$block_version       = $metadata['version'] ?? false;	$script_version      = $script_asset['version'] ?? $block_version;	$script_args         = array();	if ( 'viewScript' === $field_name && $script_uri ) {		$script_args['strategy'] = 'defer';	} 	$result = wp_register_script(		$script_handle,		$script_uri,		$script_dependencies,		$script_version,		$script_args	);	if ( ! $result ) {		return false;	} 	if ( ! empty( $metadata['textdomain'] ) && in_array( 'wp-i18n', $script_dependencies, true ) ) {		wp_set_script_translations( $script_handle, $metadata['textdomain'] );	} 	return $script_handle;}

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.5.0
The asset file is optional. Added script handle support in the asset file.from the docblock
6.1.0
Added $index parameter.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.