wppaste
WordPress

wp_enqueue_block_style( string $block_name, array $args )

Since
5.9.0
Source
wp-includes/script-loader.php:3284
Enqueues a stylesheet for a specific block.

Description

If the theme has opted-in to load block styles on demand, then the stylesheet will be enqueued on-render, otherwise when the block inits.

Compatibility

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

$block_namestring
The block-name, including namespace.
$argsarray
An array of arguments. See wp_register_style() for full information about each argument.
  • $handlestring

    The handle for the stylesheet.
  • $srcstring|false

    The source URL of the stylesheet.
  • $depsstring[]

    Array of registered stylesheet handles this stylesheet depends on.
  • $verstring|bool|null

    Stylesheet version number.
  • $mediastring

    The media for which this stylesheet has been defined.
  • $pathstring|null

    Absolute path to the stylesheet, so that it can potentially be inlined.

Performance profile

How much work a call to wp_enqueue_block_style() 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
7–13

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
2

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

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_enqueue_block_style()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always7–13none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1177–138
8.51177–138
8.41177–1384 fewer instructions than PHP 8.3
8.31217–138
8.21217–138
8.11217–138
7.41217–138

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 · 9

Used by · 2

Source code

function wp_enqueue_block_style( $block_name, $args ) {	$args = wp_parse_args(		$args,		array(			'handle' => '',			'src'    => '',			'deps'   => array(),			'ver'    => false,			'media'  => 'all',		)	); 	/**	 * Callback function to register and enqueue styles.	 *	 * @param string $content When the callback is used for the render_block filter,	 *                        the content needs to be returned so the function parameter	 *                        is to ensure the content exists.	 * @return string Block content.	 */	$callback = static function ( $content ) use ( $args ) {		// Register the stylesheet.		if ( ! empty( $args['src'] ) ) {			wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );		} 		// Add `path` data if provided.		if ( isset( $args['path'] ) ) {			wp_style_add_data( $args['handle'], 'path', $args['path'] ); 			// Get the RTL file path.			$rtl_file_path = str_replace( '.css', '-rtl.css', $args['path'] ); 			// Add RTL stylesheet.			if ( file_exists( $rtl_file_path ) ) {				wp_style_add_data( $args['handle'], 'rtl', 'replace' ); 				if ( is_rtl() ) {					wp_style_add_data( $args['handle'], 'path', $rtl_file_path );				}			}		} 		// Enqueue the stylesheet.		wp_enqueue_style( $args['handle'] ); 		return $content;	}; 	$hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';	if ( wp_should_load_block_assets_on_demand() ) {		/**		 * Callback function to register and enqueue styles.		 *		 * @param string $content The block content.		 * @param array  $block   The full block, including name and attributes.		 * @return string Block content.		 */		$callback_separate = static function ( $content, $block ) use ( $block_name, $callback ) {			if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {				return $callback( $content );			}			return $content;		}; 		/*		 * The filter's callback here is an anonymous function because		 * using a named function in this case is not possible.		 *		 * The function cannot be unhooked, however, users are still able		 * to dequeue the stylesheets registered/enqueued by the callback		 * which is why in this case, using an anonymous function		 * was deemed acceptable.		 */		add_filter( 'render_block', $callback_separate, 10, 2 );		return;	} 	/*	 * The filter's callback here is an anonymous function because

Changelog

Introduced in 5.9.0. One change between 6.7.7 and 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.

7.0.4
Parameter $args retyped from array to untyped.verified against source
5.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/script-loader.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.