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|array $fallback_gap_value = '0.5em', array|null $block_spacing = null, array $options = array() ): string
- Since
- 5.9.0, 6.1.0, 6.3.0, 6.6.0, 7.1.0
- Source
wp-includes/block-supports/layout.php:499
Compatibility
- WordPress
- since 7.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
$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_valuestring|arrayoptional- The block gap value to apply. If it's an array expected properties are "top" and/or "left". Default '0.5em'.Default:
'0.5em' $block_spacingarray|nulloptional- Custom spacing set on the block. Default null.Default:
null $optionsarrayoptional- Extra options for internal callers. Default empty array.Default:
array()$viewport_overridesarrayAn array of layout property overrides for the sake of style generation, keyed by property name.$rules_groupstring|nulldefault: nullOptional group name for the rules.$has_block_gap_overridebooldefault: falseWhether the block gap has been overridden.
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
- Trivial
- Scaling
- Constant
- Instructions
- 4
- Plugin surface
- None
- Called by
- 1
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. The body compiles to 743.
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 · 1 distinct outcome
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 |
|---|---|---|
| always | 4 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 739 | 4 | 150 | 4 fewer instructions than PHP 8.5 |
| 8.5 | 743 | 4 | 150 | |
| 8.4 | 743 | 4 | 150 | 28 fewer instructions than PHP 8.3 |
| 8.3 | 771 | 4 | 150 | |
| 8.2 | 771 | 4 | 150 | 2 fewer instructions than PHP 8.1 |
| 8.1 | 773 | 4 | 151 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 777 | 4 | 151 |
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, $options = array() ) { $base_layout = is_array( $layout ) ? $layout : array(); $viewport_overrides = $options['viewport_overrides'] ?? null; $layout_for_styles = null === $viewport_overrides ? $base_layout : array_replace( $base_layout, $viewport_overrides ); $layout_type = $base_layout['type'] ?? 'default'; $rules_group = $options['rules_group'] ?? null; $has_block_gap_override = ! empty( $options['has_block_gap_override'] ); $should_output_block_gap = null === $viewport_overrides || $has_block_gap_override; /* * Viewport styles only store changed fields. If a field is present with null, * the user cleared a value inherited from the default viewport, so check * whether the key exists rather than whether the value is truthy. */ $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) { return array_key_exists( $property, $viewport_overrides ); }; $layout_styles = array(); if ( 'default' === $layout_type ) { if ( $has_block_gap_support && $should_output_block_gap ) { if ( is_array( $gap_value ) ) { $gap_value = $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 = $layout_for_styles['contentSize'] ?? ''; $wide_size = $layout_for_styles['wideSize'] ?? ''; $justify_content = $layout_for_styles['justifyContent'] ?? 'center'; // Check if viewport-specific ("override") values exist. Null values are valid and mean the user cleared a value inherited from the default viewport. $has_justify_content_override = null !== $viewport_overrides && $has_viewport_property_override( 'justifyContent' ); $has_content_size_override = null !== $viewport_overrides && $has_viewport_property_override( 'contentSize' ); $has_wide_size_override = null !== $viewport_overrides && $has_viewport_property_override( 'wideSize' ); /* * Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific * value for contentSize or wideSize. If a viewport clears a custom constrained size, reset to the global layout variable. */ $should_output_constrained_sizes = null === $viewport_overrides || $has_content_size_override || $has_wide_size_override; $is_resetting_constrained_sizes = null !== $viewport_overrides && ( ( $has_content_size_override && ! $content_size ) || ( $has_wide_size_override && ! $wide_size ) ); // If a viewport clears a custom constrained size, reset to the global layout variable. $all_max_width_value = $content_size ? $content_size : ( $wide_size && ! $has_content_size_override ? $wide_size : 'var(--wp--style--global--content-size, none)' ); $wide_max_width_value = $wide_size ? $wide_size : ( $content_size && ! $has_wide_size_override ? $content_size : 'var(--wp--style--global--wide-size, none)' );Changelog
Introduced in 5.9.0. 2 changes between 6.7.7 and 7.1.0.
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 7.1.0 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.