wppaste
WordPress

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
Generates the CSS corresponding to the provided layout.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
23–87

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

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

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!isset($layout) && $layout_type !== "default" && $layout_type !== "constrained" && empty($layout_styles)23–83none
!isset($layout) && $layout_type !== "default" && $layout_type !== "constrained" && !empty($layout_styles)27–87wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev46423–8771
8.546423–8771
8.446423–877128 fewer instructions than PHP 8.3
8.349223–8971
8.249223–89714 fewer instructions than PHP 8.1
8.149623–907312 fewer instructions than PHP 7.4
7.450823–9073

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

Used by · 1

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.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

7.1.3
Parameter $options added.verified against source
7.0.7
Parameter $fallback_gap_value retyped from string to string|array.verified against source
6.6.0
Removed duplicated selector from layout styles.
Enabled negative margins for alignfull children of blocks with custom padding.from the docblock
6.3.0
Added grid layout type.from the docblock
6.1.0
Added $block_spacing param, use style engine to enqueue styles.from the docblock
5.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.