_wp_multiple_block_styles() WordPress Function

The _wp_multiple_block_styles() function allows you to register multiple block styles for a single block. This is useful if you want to provide different styles for a block, based on different criteria. For example, you could use this function to register a block style for a block that is being used in a sidebar, and another block style for the same block when it is being used in the content area of a post.

_wp_multiple_block_styles( array $metadata ) #

Allows multiple block styles.


Parameters

$metadata

(array)(Required)Metadata for registering a block type.


Top ↑

Return

(array) Metadata for registering a block type.


Top ↑

Source

File: wp-includes/blocks.php

function _wp_multiple_block_styles( $metadata ) {
	foreach ( array( 'style', 'editorStyle' ) as $key ) {
		if ( ! empty( $metadata[ $key ] ) && is_array( $metadata[ $key ] ) ) {
			$default_style = array_shift( $metadata[ $key ] );
			foreach ( $metadata[ $key ] as $handle ) {
				$args = array( 'handle' => $handle );
				if ( 0 === strpos( $handle, 'file:' ) && isset( $metadata['file'] ) ) {
					$style_path      = remove_block_asset_path_prefix( $handle );
					$theme_path_norm = wp_normalize_path( get_theme_file_path() );
					$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
					$is_theme_block  = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $theme_path_norm );

					$style_uri = plugins_url( $style_path, $metadata['file'] );

					if ( $is_theme_block ) {
						$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
					}

					$args = array(
						'handle' => sanitize_key( "{$metadata['name']}-{$style_path}" ),
						'src'    => $style_uri,
					);
				}

				wp_enqueue_block_style( $metadata['name'], $args );
			}

			// Only return the 1st item in the array.
			$metadata[ $key ] = $default_style;
		}
	}
	return $metadata;
}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.