wp_get_layout_style( string $selector, array $layout, bool $has_block_gap_support = false, string|string[]|null $gap_value = null, bool $should_skip_gap_serialization = false, string $fallback_gap_value = '0.5em', array|null $block_spacing = null ): string
- Since
- 5.9.0, 6.1.0, 6.3.0, 6.6.0
- Source
wp-includes/block-supports/layout.php:240
Compatibility
- WordPress
- since 6.6.0
- PHP
- 7.4–8.6-dev
- 6.7.10
- 6.8.11
- 6.9.10
- 7.0.7
- 7.1.3
Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$selectorstring- CSS selector.
$layoutarray- Layout object. The one that is passed has already checked the existence of default block layout.
$has_block_gap_supportbooloptional- Whether the theme has support for the block gap. Default false.Default:
false $gap_valuestring|string[]|nulloptional- The block gap value to apply. Default null.Default:
null $should_skip_gap_serializationbooloptional- Whether to skip applying the user-defined value set in the editor. Default false.Default:
false $fallback_gap_valuestringoptional- The block gap value to apply. Default '0.5em'.Default:
'0.5em' $block_spacingarray|nulloptional- Custom spacing set on the block. Default null.Default:
null
Return value
string- CSS styles on success. Else, empty string.
Performance profile
How much work a call to wp_get_layout_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
- Light
- Scaling
- Scales with input
- Instructions
- 23–87
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 464.
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
- hookthird-party callbacks
apply_filters()one call below wp_get_layout_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 · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_layout_style() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($layout) && $layout_type !== "default" && $layout_type !== "constrained" && empty($layout_styles) | 23–83 | none |
!isset($layout) && $layout_type !== "default" && $layout_type !== "constrained" && !empty($layout_styles) | 27–87 | wp_style_engine_get_stylesheet_from_css_rules() |
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 | 464 | 23–87 | 71 | |
| 8.5 | 464 | 23–87 | 71 | |
| 8.4 | 464 | 23–87 | 71 | 28 fewer instructions than PHP 8.3 |
| 8.3 | 492 | 23–89 | 71 | |
| 8.2 | 492 | 23–89 | 71 | 4 fewer instructions than PHP 8.1 |
| 8.1 | 496 | 23–90 | 73 | 12 fewer instructions than PHP 7.4 |
| 7.4 | 508 | 23–90 | 73 |
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 · 5
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- _wp_to_kebab_case()This function is trying to replicate what lodash's kebabCase (JS library) does in the client.
- safecss_filter_attr()Filters an inline style attribute and removes disallowed rules.
- wp_style_engine_get_styles()Global public interface method to generate styles from a single style object, e.g. the value of a block's attributes.style object or the top level styles in theme.json.
- wp_style_engine_get_stylesheet_from_css_rules()Returns compiled CSS from a collection of selectors and declarations.
Used by · 1
- wp_render_layout_support_flag()Renders the layout config to the block wrapper.
Source code
function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em', $block_spacing = null ) { $layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default'; $layout_styles = array(); if ( 'default' === $layout_type ) { if ( $has_block_gap_support ) { if ( is_array( $gap_value ) ) { $gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null; } if ( null !== $gap_value && ! $should_skip_gap_serialization ) { // Get spacing CSS variable from preset value if provided. if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) { $index_to_splice = strrpos( $gap_value, '|' ) + 1; $slug = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) ); $gap_value = "var(--wp--preset--spacing--$slug)"; } array_push( $layout_styles, array( 'selector' => "$selector > *", 'declarations' => array( 'margin-block-start' => '0', 'margin-block-end' => '0', ), ), array( 'selector' => "$selector > * + *", 'declarations' => array( 'margin-block-start' => $gap_value, 'margin-block-end' => '0', ), ) ); } } } elseif ( 'constrained' === $layout_type ) { $content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : ''; $wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : ''; $justify_content = isset( $layout['justifyContent'] ) ? $layout['justifyContent'] : 'center'; $all_max_width_value = $content_size ? $content_size : $wide_size; $wide_max_width_value = $wide_size ? $wide_size : $content_size; // Make sure there is a single CSS rule, and all tags are stripped for security. $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] ); $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] ); $margin_left = 'left' === $justify_content ? '0 !important' : 'auto !important'; $margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important'; if ( $content_size || $wide_size ) { array_push( $layout_styles, array( 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))", 'declarations' => array( 'max-width' => $all_max_width_value, 'margin-left' => $margin_left, 'margin-right' => $margin_right, ), ), array( 'selector' => "$selector > .alignwide", 'declarations' => array( 'max-width' => $wide_max_width_value ), ), array( 'selector' => "$selector .alignfull", 'declarations' => array( 'max-width' => 'none' ), ) ); } if ( isset( $block_spacing ) ) { $block_spacing_values = wp_style_engine_get_styles( array( 'spacing' => $block_spacing, ) );Changelog
Introduced in 5.9.0. 2 changes between 6.7.10 and 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
$options added.verified against source$fallback_gap_value retyped from string to string|array.verified against sourceEnabled negative margins for alignfull children of blocks with custom padding.from the docblock
$block_spacing param, use style engine to enqueue styles.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.10 tag, from
src/wp-includes/block-supports/layout.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.