On this page
wp_style_engine_get_stylesheet_from_css_rules( array $css_rules, array $options = array() ): string
Returns compiled CSS from a collection of selectors and declarations.
Description
Useful for returning a compiled stylesheet from any collection of CSS selector + declarations.
Example usage: $css_rules = array( array( ‘selector’ => ‘.elephant-are-cool’, ‘declarations’ => array( ‘color’ => ‘gray’, ‘width’ => ‘3em’ ) ) ); $css = wp_style_engine_get_stylesheet_from_css_rules( $css_rules ); // Returns .elephant-are-cool{color:gray;width:3em}.
Parameters
$css_rulesarray Required-
Required. A collection of CSS rules.
...$0arrayselectorstringA CSS selector.declarationsstring[]An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
$optionsarray OptionalAn array of options.
contextstring|nullAn identifier describing the origin of the style object, e.g.,'block-supports'or'global-styles'. Default is'block-supports'.
When set, the style engine will attempt to store the CSS rules.optimizeboolWhether to optimize the CSS output, e.g., combine rules. Default isfalse.prettifyboolWhether to add new lines and indents to output. Default is the test of whether the global constantSCRIPT_DEBUGis defined.
Default:
array()Return
string A string of compiled CSS declarations, or empty string.
Source
File:
wp-includes/style-engine.php. View all referencesfunction wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = array() ) { if ( empty( $css_rules ) ) { return ''; } $options = wp_parse_args( $options, array( 'context' => null, ) ); $css_rule_objects = array(); foreach ( $css_rules as $css_rule ) { if ( empty( $css_rule['selector'] ) || empty( $css_rule['declarations'] ) || ! is_array( $css_rule['declarations'] ) ) { continue; } if ( ! empty( $options['context'] ) ) { WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'] ); } $css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'] ); } if ( empty( $css_rule_objects ) ) { return ''; } return WP_Style_Engine::compile_stylesheet_from_css_rules( $css_rule_objects, $options ); }Related
Uses
Uses Description WP_Style_Engine_CSS_Rule::__construct() wp-includes/style-engine/class-wp-style-engine-css-rule.php Constructor
WP_Style_Engine::compile_stylesheet_from_css_rules() wp-includes/style-engine/class-wp-style-engine.php Returns a compiled stylesheet from stored CSS rules.
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_stylesheet_from_css_rules