_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.
Return
(array) Metadata for registering a block type.
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; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
5.9.0 | Introduced. |