Title: wp_style_engine_get_stylesheet_from_css_rules
Published: November 2, 2022
Last modified: February 24, 2026

---

# wp_style_engine_get_stylesheet_from_css_rules( array $css_rules, array $options = array() ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#wp--skip-link--target)

Returns compiled CSS from a collection of selectors and declarations.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#description)󠁿

Useful for returning a compiled stylesheet from any collection of CSS selector +
declarations.

Example usage:

    ```php
    $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:

    ```php
    .elephant-are-cool{color:gray;width:3em}
    ```

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#parameters)󠁿

 `$css_rules`arrayrequired

Required. A collection of CSS rules.

 * `...$0` array
    - `rules_group` string
    - A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such
      as `@media (min-width: 80rem)` or `@layer module`.
    - `selector` string
    - A CSS selector.
    - `declarations` string[]
    - An associative array of CSS definitions, e.g. `array( "$property" => "$value","
      $property" => "$value" )`.

`$options`arrayoptional

An array of options.

 * `context` string|null
 * An identifier describing the origin of the style object, e.g. `'block-supports'`
   or `'global-styles'`. Default `'block-supports'`.
    When set, the style engine
   will attempt to store the CSS rules.
 * `optimize` bool
 * Whether to optimize the CSS output, e.g. combine rules.
    Default false.
 * `prettify` bool
 * Whether to add new lines and indents to output.
    Defaults to whether the `SCRIPT_DEBUG`
   constant is defined.

Default:`array()`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#return)󠁿

 string A string of compiled CSS declarations, or empty string.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#source)󠁿

    ```php
    function 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;
    		}

    		$rules_group = $css_rule['rules_group'] ?? null;
    		if ( ! empty( $options['context'] ) ) {
    			WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'], $rules_group );
    		}

    		$css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'], $rules_group );
    	}

    	if ( empty( $css_rule_objects ) ) {
    		return '';
    	}

    	return WP_Style_Engine::compile_stylesheet_from_css_rules( $css_rule_objects, $options );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/style-engine.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/style-engine.php#L142)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/style-engine.php#L142-L173)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Style_Engine_CSS_Rule::__construct()](https://developer.wordpress.org/reference/classes/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()](https://developer.wordpress.org/reference/classes/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()](https://developer.wordpress.org/reference/classes/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()](https://developer.wordpress.org/reference/functions/wp_parse_args/)`wp-includes/functions.php` |

Merges user defined arguments into defaults array.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#)

| Used by | Description | 
| [WP_Duotone::output_block_styles()](https://developer.wordpress.org/reference/classes/wp_duotone/output_block_styles/)`wp-includes/class-wp-duotone.php` |

Appends the used block duotone filter declarations to the inline block supports CSS.

  | 
| [WP_Duotone::output_footer_assets()](https://developer.wordpress.org/reference/classes/wp_duotone/output_footer_assets/)`wp-includes/class-wp-duotone.php` |

Outputs all necessary SVG for duotone filters, CSS for classic themes.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.6.0](https://developer.wordpress.org/reference/since/6.6.0/) | Added support for `$rules_group` in the `$css_rules` array. | 
| [6.1.0](https://developer.wordpress.org/reference/since/6.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_style_engine_get_stylesheet_from_css_rules%2F)
before being able to contribute a note or feedback.