wp_enqueue_block_style( string $block_name, array $args )
- Since
- 5.9.0
- Source
wp-includes/script-loader.php:3318
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.
$handlestringThe handle for the stylesheet.$srcstring|falseThe source URL of the stylesheet.$depsstring[]Array of registered stylesheet handles this stylesheet depends on.$verstring|bool|nullStylesheet version number.$mediastringThe media for which this stylesheet has been defined.$pathstring|nullAbsolute 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
- Scaling
- Constant
- Instructions
- 7–13
- Plugin surface
- None
- Called by
- 2
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.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–13 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 117 | 7–13 | 8 | |
| 8.5 | 117 | 7–13 | 8 | |
| 8.4 | 117 | 7–13 | 8 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 121 | 7–13 | 8 | |
| 8.2 | 121 | 7–13 | 8 | |
| 8.1 | 121 | 7–13 | 8 | |
| 7.4 | 121 | 7–13 | 8 |
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
- wp_parse_args()Merges user defined arguments into defaults array.
- 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).
- wp_enqueue_style()Enqueues a CSS stylesheet.
- did_action()Retrieves the number of times an action has been fired during the current request.
- wp_should_load_block_assets_on_demand()Checks whether block styles should be loaded only on-render.
- add_filter()Adds a callback function to a filter hook.
- add_action()Adds a callback function to an action hook.
Used by · 2
- enqueue_legacy_post_comments_block_styles()Enqueues styles from the legacy `core/post-comments` block. These styles are required only by the block's fallback.
- twentytwentyfour_block_stylesheets()Enqueues custom block stylesheets.
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 becauseChangelog
Introduced in 5.9.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$args retyped from array to untyped.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.