block-style-variations.php — Block Style Variation Support
Enables per-section styling of blocks via block style variations defined in theme.json.
Source: wp-includes/block-supports/block-style-variations.php
Since: 6.6.0
Support Key
Registered as block-style-variation with an empty config array. Variations are detected via CSS class names matching is-style-{name} on blocks.
Functions
wp_get_block_style_variation_name_from_class()
function wp_get_block_style_variation_name_from_class( string $class_string ): ?arraySince: 6.6.0
Extracts block style variation names from a CSS class string using regex /bis-style-(?!default)(S+)b/. Returns array of matched variation slugs, or null if input is not a string.
wp_resolve_block_style_variation_ref_values()
function wp_resolve_block_style_variation_ref_values( array &$variation_data, array $theme_json )Since: 6.6.0
Access: private
Recursively resolves ref values within variation data by looking up the referenced path in theme.json data. Invalid or unresolvable refs are unset.
wp_render_block_style_variation_support_styles()
function wp_render_block_style_variation_support_styles( array $parsed_block ): arraySince: 6.6.0
Access: private
Processing flow:
- Extracts variation names from
classNameattribute viawp_get_block_style_variation_name_from_class() - Looks up variation data in merged theme.json (
WP_Theme_JSON_Resolver::get_merged_data()) understyles.blocks.{blockName}.variations.{variation} - Only the first variation with data is processed
- Resolves
refvalues viawp_resolve_block_style_variation_ref_values() - Generates a unique instance ID via
wp_unique_id() - Relocates root variation styles under a
blocksproperty to leverage custom selectors - Creates a
WP_Theme_JSONinstance and generates a scoped stylesheet - Temporarily registers/unregisters the variation instance in
WP_Block_Styles_Registry - Temporarily removes
wp_filter_out_block_nodesfilter (non-admin only) to include block nodes - Enqueues styles as inline CSS on
block-style-variation-styleshandle (depends onwp-block-libraryandglobal-styles) - Appends the unique instance class to
className
wp_render_block_style_variation_class_name()
function wp_render_block_style_variation_class_name( string $block_content, array $block ): stringSince: 6.6.0
Access: private
Ensures the variation instance class name (pattern: is-style-{slug}--{id}) is applied to the block’s first HTML tag via WP_HTML_Tag_Processor.
wp_enqueue_block_style_variation_styles()
function wp_enqueue_block_style_variation_styles()Since: 6.6.0
Access: private
Enqueues the block-style-variation-styles stylesheet.
wp_register_block_style_variations_from_theme_json_partials()
function wp_register_block_style_variations_from_theme_json_partials( array $variations )Since: 6.6.0
Access: private
Registers block style variations from theme.json partial data. For each variation with blockTypes and styles, registers the style via register_block_style() if not already registered. Uses slug (or kebab-cased title) as the variation name.
Hooks
| Hook | Type | Priority | Callback |
|---|---|---|---|
render_block_data | filter | 10 | wp_render_block_style_variation_support_styles |
render_block | filter | 10 | wp_render_block_style_variation_class_name |
wp_enqueue_scripts | action | 1 | wp_enqueue_block_style_variation_styles |
Registration
WP_Block_Supports::get_instance()->register( 'block-style-variation', array() );