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
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
- Scaling
- Constant
- Instructions
- 6–111
- Plugin surface
- None
- 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, depending on the branch taken. The body compiles to 117.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–12 | none |
!empty($metadata[$field_name]) && $script_handle_or_path === false | 16–20 | remove_block_asset_path_prefix() |
!empty($metadata[$field_name]) && $script_handle_or_path !== false && !$script_asset && wp_script_is() | 46–52 | remove_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–60 | remove_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–97 | remove_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–105 | remove_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_dependencies | 91–103 | remove_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_dependencies | 99–111 | remove_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 115 | 6–109 | 15 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 117 | 6–111 | 15 | |
| 8.4 | 117 | 6–111 | 15 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 122 | 6–116 | 15 | |
| 8.2 | 122 | 6–116 | 15 | |
| 8.1 | 122 | 6–116 | 15 | |
| 7.4 | 122 | 6–116 | 15 |
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
- remove_block_asset_path_prefix()Removes the block asset's path prefix if provided.
- wp_normalize_path()Normalizes a filesystem path.
- generate_block_asset_handle()Generates the name for an asset based on the name of the block and the field name provided.
- wp_script_is()Determines whether a script has been added to the queue.
- get_block_asset_url()Gets the URL to a block asset.
- wp_register_script()Registers a new script.
- wp_set_script_translations()Sets translated strings for a script.
Used by · 1
- register_block_type_from_metadata()Registers a block type from the metadata stored in the `block.json` file.
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.
Signature, return type and hooks compared across 5 parsed releases.
$index parameter.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.