register_block_script_module_id( array $metadata, string $field_name, int $index = 0 ): string|false
- Since
- 6.5.0
- Source
wp-includes/blocks.php:169
Description
Detects when a path to a file was provided and optionally finds a corresponding asset file with details necessary to register the script module with an automatically generated module ID. It returns the unprocessed script module ID otherwise.
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 module ID to register when multiple items passed. Default 0.Default:
0
Return value
string|false- Script module ID or false on failure.
Performance profile
How much work a call to register_block_script_module_id() 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–124
- 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 128.
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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost register_block_script_module_id() 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]) && $module_id === false | 16–20 | remove_block_asset_path_prefix() |
!empty($metadata[$field_name]) && $module_id !== false | 85–119 | remove_block_asset_path_prefix(), substr_replace(), generate_block_asset_handle(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_script_module() |
!empty($metadata[$field_name]) && $module_id !== false | 96–124 | remove_block_asset_path_prefix(), substr_replace(), generate_block_asset_handle(), realpath(), wp_normalize_path(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_interactivity(), ->add_client_navigation_support_to_script_module(), wp_register_script_module() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 127 | 6–123 | 16 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 128 | 6–124 | 16 | |
| 8.4 | 128 | 6–124 | 16 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 130 | 6–126 | 16 | |
| 8.2 | 130 | 6–126 | 16 | |
| 8.1 | 130 | 6–126 | 16 | |
| 7.4 | 130 | 6–126 | 16 |
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.
- generate_block_asset_handle()Generates the name for an asset based on the name of the block and the field name provided.
- wp_normalize_path()Normalizes a filesystem path.
- get_block_asset_url()Gets the URL to a block asset.
- wp_interactivity()Retrieves the main WP_Interactivity_API instance.
- wp_register_script_module()Registers the script module if no script module with that script module identifier has already been registered.
- wp_interactivity()::add_client_navigation_support_to_script_module()
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_module_id( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } $module_id = $metadata[ $field_name ]; if ( is_array( $module_id ) ) { if ( empty( $module_id[ $index ] ) ) { return false; } $module_id = $module_id[ $index ]; } $module_path = remove_block_asset_path_prefix( $module_id ); if ( $module_id === $module_path ) { return $module_id; } $path = dirname( $metadata['file'] ); $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) ); $module_id = generate_block_asset_handle( $metadata['name'], $field_name, $index ); $module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) ); $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) ); $module_uri = get_block_asset_url( $module_path_norm ); /** @var array{ dependencies?: list<non-falsy-string|array{id: non-falsy-string, import?: 'static'|'dynamic'}>, version?: string|false|null, ... } $module_asset */ $module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array(); $module_dependencies = $module_asset['dependencies'] ?? array(); $block_version = $metadata['version'] ?? false; $module_version = $module_asset['version'] ?? $block_version; $supports_interactivity_true = isset( $metadata['supports']['interactivity'] ) && true === $metadata['supports']['interactivity']; $is_interactive = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['interactive'] ) && true === $metadata['supports']['interactivity']['interactive'] ); $supports_client_navigation = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['clientNavigation'] ) && true === $metadata['supports']['interactivity']['clientNavigation'] ); $args = array(); // Blocks using the Interactivity API are server-side rendered, so they are // by design not in the critical rendering path and should be deprioritized. if ( $is_interactive ) { $args['fetchpriority'] = 'low'; $args['in_footer'] = true; } // Blocks using the Interactivity API that support client-side navigation // must be marked as such in their script modules. if ( $is_interactive && $supports_client_navigation ) { wp_interactivity()->add_client_navigation_support_to_script_module( $module_id ); } wp_register_script_module( $module_id, $module_uri, $module_dependencies, $module_version, $args ); return $module_id;}Changelog
Introduced in 6.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
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.