On this page
wp_style_engine_get_styles( array $block_styles, array $options = array() ): array
Global public interface method to generate styles from a single style object, e.g., the value of a block’s attributes.style object or the top level styles in theme.json.
Description
See: https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles and https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
Example usage:
$styles = wp_style_engine_get_styles( array( ‘color’ => array( ‘text’ => ‘#cccccc’ ) ) ); // Returns array( 'css' => 'color: #cccccc', 'declarations' => array( 'color' => '#cccccc' ), 'classnames' => 'has-color' ).
Parameters
$block_stylesarray Required-
The style object.
$optionsarray Optional-
An array of options.
contextstring|nullAn identifier describing the origin of the style object, e.g.,'block-supports'or'global-styles'. Default isnull.
When set, the style engine will attempt to store the CSS rules, where a selector is also passed.convert_vars_to_classnamesboolWhether to skip converting incoming CSS var patterns, e.g.,var:preset|<PRESET_TYPE>|<PRESET_SLUG>, to var( --wp--preset--* ) values. Defaultfalse.selectorstringOptional. When a selector is passed, the value of$cssin the return value will comprise a full CSS rule$selector { ...$css_declarations }, otherwise, the value will be a concatenated string of CSS declarations.
Default:
array()
Return
array
cssstringA CSS ruleset or declarations block formatted to be placed in an HTMLstyleattribute or tag.declarationsstring[]An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).classnamesstringClassnames separated by a space.
Source
File: wp-includes/style-engine.php. View all references
function wp_style_engine_get_styles( $block_styles, $options = array() ) {
$options = wp_parse_args(
$options,
array(
'selector' => null,
'context' => null,
'convert_vars_to_classnames' => false,
)
);
$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options );
// Output.
$styles_output = array();
if ( ! empty( $parsed_styles['declarations'] ) ) {
$styles_output['css'] = WP_Style_Engine::compile_css( $parsed_styles['declarations'], $options['selector'] );
$styles_output['declarations'] = $parsed_styles['declarations'];
if ( ! empty( $options['context'] ) ) {
WP_Style_Engine::store_css_rule( $options['context'], $options['selector'], $parsed_styles['declarations'] );
}
}
if ( ! empty( $parsed_styles['classnames'] ) ) {
$styles_output['classnames'] = implode( ' ', array_unique( $parsed_styles['classnames'] ) );
}
return array_filter( $styles_output );
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Style_Engine::parse_block_styles() wp-includes/style-engine/class-wp-style-engine.php | Returns classnames and CSS based on the values in a styles object. |
| WP_Style_Engine::compile_css() wp-includes/style-engine/class-wp-style-engine.php | Returns compiled CSS from css_declarations. |
| WP_Style_Engine::store_css_rule() wp-includes/style-engine/class-wp-style-engine.php | Stores a CSS rule using the provided CSS selector and CSS declarations. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
Changelog
| Version | Description |
|---|---|
| 6.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_style_engine_get_styles