wppaste
WordPress

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
Finds a style handle for the block metadata field.

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

Reads or writes the filesystem via filemtime().

Scaling
Constant

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

Instructions
31–129

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

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 it touches

  • filesystemfilesystem accessfilemtime()called directly
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!empty($metadata[$field_name]) && !is_array($style_handle) && !wp_style_is() && !isset($metadata) && !wp_should_load_separate_core_block_assets()31generate_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–38generate_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–41generate_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–88generate_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–91generate_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–91generate_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–94generate_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–104generate_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–107generate_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–108generate_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–107generate_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–111generate_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–110generate_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–111generate_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–114generate_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–123generate_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–126generate_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–126generate_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–129generate_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

PHPCompiledExecutedBranchesNotes
8.6-dev16231–12926
8.516231–12926
8.416231–1292613 fewer instructions than PHP 8.3
8.317531–13526
8.217531–13526
8.117531–135265 fewer instructions than PHP 7.4
7.418031–13826

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

Used by · 1

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.

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