register_block_style_handle( array $metadata, string $field_name, int $index = 0 ): string|false
- Since
- 5.5.0, 6.1.0
- Source
wp-includes/blocks.php:360
Description
Detects when a path to a file was provided and registers the style under an automatically generated handle name. It returns the unprocessed style handle otherwise, except for the first style of a core block, which is instead registered from the block's own stylesheet when separate core block assets are loaded. Core blocks accept only handles, not paths.
Compatibility
- WordPress
- since 6.1.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 style to register when multiple items passed.
Default 0.Default:0
Return value
string|false- Style handle provided directly or created through style's registration, or false on failure.
Performance profile
How much work a call to register_block_style_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
- Heavy
- Scaling
- Constant
- Instructions
- 31–129
- Plugin surface
- None
- Called by
- 1
Reads or writes the filesystem via filemtime().
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 162.
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 it touches
- filesystemfilesystem access
filemtime()called directly - hookthird-party callbacks
apply_filters()one call below register_block_style_handle()
Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 19 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost register_block_style_handle() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && !wp_should_load_separate_core_block_assets() | 31 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) | 35–38 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() | 38–41 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() | 69–88 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() | 72–91 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && defined('SCRIPT_DEBUG') | 76–91 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() && defined('SCRIPT_DEBUG') | 79–94 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !is_rtl() | 81–104 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style(), wp_style_add_data(), is_rtl() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() && !is_rtl() | 84–107 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style(), wp_style_add_data(), is_rtl() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && is_rtl() && !file_exists() | 85–108 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && defined('SCRIPT_DEBUG') && !is_rtl() | 88–107 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style(), wp_style_add_data(), is_rtl() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() && is_rtl() && !file_exists() | 88–111 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists() |
7 further outcomes, up to 129 instructions
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() && defined('SCRIPT_DEBUG') && !is_rtl() | 91–110 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style(), wp_style_add_data(), is_rtl() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && defined('SCRIPT_DEBUG') && is_rtl() && !file_exists() | 92–111 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() && defined('SCRIPT_DEBUG') && is_rtl() && !file_exists() | 95–114 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && is_rtl() && file_exists() | 100–123 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists(), wp_style_add_data(), wp_style_add_data(), wp_style_add_data() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() && is_rtl() && file_exists() | 103–126 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists(), wp_style_add_data(), wp_style_add_data(), wp_style_add_data() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && defined('SCRIPT_DEBUG') && is_rtl() && file_exists() | 107–126 | generate_block_asset_handle(), wp_style_is(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists(), wp_style_add_data(), wp_style_add_data(), wp_style_add_data() |
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && wp_should_load_separate_core_block_assets() && defined('SCRIPT_DEBUG') && is_rtl() && file_exists() | 110–129 | generate_block_asset_handle(), wp_style_is(), wp_should_load_separate_core_block_assets(), remove_block_asset_path_prefix(), realpath(), wp_normalize_path(), get_block_asset_url(), filemtime(), wp_register_style(), wp_style_add_data(), is_rtl(), file_exists(), wp_style_add_data(), wp_style_add_data(), wp_style_add_data() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 162 | 31–129 | 26 | |
| 8.5 | 162 | 31–129 | 26 | |
| 8.4 | 162 | 31–129 | 26 | 13 fewer instructions than PHP 8.3 |
| 8.3 | 175 | 31–135 | 26 | |
| 8.2 | 175 | 31–135 | 26 | |
| 8.1 | 175 | 31–135 | 26 | 5 fewer instructions than PHP 7.4 |
| 7.4 | 180 | 31–138 | 26 |
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 · 10
- generate_block_asset_handle()Generates the name for an asset based on the name of the block and the field name provided.
- wp_style_is()Checks whether a CSS stylesheet has been added to the queue.
- wp_normalize_path()Normalizes a filesystem path.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_should_load_separate_core_block_assets()Checks whether separate styles should be loaded for core blocks.
- remove_block_asset_path_prefix()Removes the block asset's path prefix if provided.
- get_block_asset_url()Gets the URL to a block asset.
- wp_register_style()Registers a CSS stylesheet.
- wp_style_add_data()Adds metadata to a CSS stylesheet.
- is_rtl()Determines whether the current locale is right-to-left (RTL).
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_style_handle( $metadata, $field_name, $index = 0 ) { if ( empty( $metadata[ $field_name ] ) ) { return false; } $style_handle = $metadata[ $field_name ]; if ( is_array( $style_handle ) ) { if ( empty( $style_handle[ $index ] ) ) { return false; } $style_handle = $style_handle[ $index ]; } $style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index ); // If the style handle is already registered, skip re-registering. if ( wp_style_is( $style_handle_name, 'registered' ) ) { return $style_handle_name; } static $wpinc_path_norm = ''; if ( ! $wpinc_path_norm ) { $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) ); } $is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm ); // Skip registering individual styles for each core block when a bundled version provided. if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) { return false; } $style_path = remove_block_asset_path_prefix( $style_handle ); $is_style_handle = $style_handle === $style_path; // Allow only passing style handles for core blocks. if ( $is_core_block && ! $is_style_handle ) { return false; } // Return the style handle unless it's the first item for every core block that requires special treatment. if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) { return $style_handle; } // Check whether styles should have a ".min" suffix or not. $suffix = SCRIPT_DEBUG ? '' : '.min'; if ( $is_core_block ) { $style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css"; } $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) ); $style_uri = get_block_asset_url( $style_path_norm ); $block_version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; $version = $style_path_norm && defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( $style_path_norm ) : $block_version; $result = wp_register_style( $style_handle_name, $style_uri, array(), $version ); if ( ! $result ) { return false; } if ( $style_uri ) { wp_style_add_data( $style_handle_name, 'path', $style_path_norm ); if ( $is_core_block ) { $rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm ); } else { $rtl_file = str_replace( '.css', '-rtl.css', $style_path_norm ); } if ( is_rtl() && file_exists( $rtl_file ) ) { wp_style_add_data( $style_handle_name, 'rtl', 'replace' ); wp_style_add_data( $style_handle_name, 'suffix', $suffix ); wp_style_add_data( $style_handle_name, 'path', $rtl_file ); } } return $style_handle_name;}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.