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

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_overridesarray

    An array of layout property overrides for the sake of style generation, keyed by property name.
  • $rules_groupstring|nulldefault: null

    Optional group name for the rules.
  • $has_block_gap_overridebooldefault: false

    Whether 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4

Executed per call on PHP 8.5. The body compiles to 743.

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

WhenInstructionsCalls it makes
always4none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev73941504 fewer instructions than PHP 8.5
8.57434150
8.4743415028 fewer instructions than PHP 8.3
8.37714150
8.277141502 fewer instructions than PHP 8.1
8.177341514 fewer instructions than PHP 7.4
7.47774151

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, $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.

  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.1.0
Parameter $options added.verified against source
7.1.0
Added options array with options to process responsive styles.from the docblock
7.0.4
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 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.