wp_enqueue_stored_styles

The timeline below displays how wordpress function wp_enqueue_stored_styles has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
 * Styles are stored via the style engine API.
 *
 * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
 *
 * @since 6.1.0
 *
 * @param array $options {
 *     Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context().
 *     Default empty array.
 *
 *     @type bool $optimize Whether to optimize the CSS output, e.g., combine rules.
 *                          Default false.
 *     @type bool $prettify Whether to add new lines and indents to output.
 *                          Default to whether the `SCRIPT_DEBUG` constant is defined.
 * }
 */
function wp_enqueue_stored_styles($options = array())
{
    $is_block_theme = wp_is_block_theme();
    $is_classic_theme = !$is_block_theme;
    /*
     * For block themes, this function prints stored styles in the header.
     * For classic themes, in the footer.
     */
    if ($is_block_theme && doing_action('wp_footer') || $is_classic_theme && doing_action('wp_enqueue_scripts')) {
        return;
    }
    $core_styles_keys = array('block-supports');
    $compiled_core_stylesheet = '';
    $style_tag_id = 'core';
    // Adds comment if code is prettified to identify core styles sections in debugging.
    $should_prettify = isset($options['prettify']) ? true === $options['prettify'] : (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
    foreach ($core_styles_keys as $style_key) {
        if ($should_prettify) {
            $compiled_core_stylesheet .= "/**\n * Core styles: {$style_key}\n */\n";
        }
        // Chains core store ids to signify what the styles contain.
        $style_tag_id .= '-' . $style_key;
        $compiled_core_stylesheet .= wp_style_engine_get_stylesheet_from_context($style_key, $options);
    }
    // Combines Core styles.
    if (!empty($compiled_core_stylesheet)) {
        wp_register_style($style_tag_id, false);
        wp_add_inline_style($style_tag_id, $compiled_core_stylesheet);
        wp_enqueue_style($style_tag_id);
    }
    // Prints out any other stores registered by themes or otherwise.
    $additional_stores = WP_Style_Engine_CSS_Rules_Store::get_stores();
    foreach (array_keys($additional_stores) as $store_name) {
        if (in_array($store_name, $core_styles_keys, true)) {
            continue;
        }
        $styles = wp_style_engine_get_stylesheet_from_context($store_name, $options);
        if (!empty($styles)) {
            $key = "wp-style-engine-{$store_name}";
            wp_register_style($key, false);
            wp_add_inline_style($key, $styles);
            wp_enqueue_style($key);
        }
    }
}

WordPress Version: 6.3

/**
 * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
 * Styles are stored via the style engine API.
 *
 * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
 *
 * @since 6.1.0
 *
 * @param array $options {
 *     Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context().
 *     Default empty array.
 *
 *     @type bool $optimize Whether to optimize the CSS output, e.g., combine rules.
 *                          Default true.
 *     @type bool $prettify Whether to add new lines and indents to output.
 *                          Default to whether the `SCRIPT_DEBUG` constant is defined.
 * }
 */
function wp_enqueue_stored_styles($options = array())
{
    $is_block_theme = wp_is_block_theme();
    $is_classic_theme = !$is_block_theme;
    /*
     * For block themes, this function prints stored styles in the header.
     * For classic themes, in the footer.
     */
    if ($is_block_theme && doing_action('wp_footer') || $is_classic_theme && doing_action('wp_enqueue_scripts')) {
        return;
    }
    $core_styles_keys = array('block-supports');
    $compiled_core_stylesheet = '';
    $style_tag_id = 'core';
    // Adds comment if code is prettified to identify core styles sections in debugging.
    $should_prettify = isset($options['prettify']) ? true === $options['prettify'] : (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
    foreach ($core_styles_keys as $style_key) {
        if ($should_prettify) {
            $compiled_core_stylesheet .= "/**\n * Core styles: {$style_key}\n */\n";
        }
        // Chains core store ids to signify what the styles contain.
        $style_tag_id .= '-' . $style_key;
        $compiled_core_stylesheet .= wp_style_engine_get_stylesheet_from_context($style_key, $options);
    }
    // Combines Core styles.
    if (!empty($compiled_core_stylesheet)) {
        wp_register_style($style_tag_id, false);
        wp_add_inline_style($style_tag_id, $compiled_core_stylesheet);
        wp_enqueue_style($style_tag_id);
    }
    // Prints out any other stores registered by themes or otherwise.
    $additional_stores = WP_Style_Engine_CSS_Rules_Store::get_stores();
    foreach (array_keys($additional_stores) as $store_name) {
        if (in_array($store_name, $core_styles_keys, true)) {
            continue;
        }
        $styles = wp_style_engine_get_stylesheet_from_context($store_name, $options);
        if (!empty($styles)) {
            $key = "wp-style-engine-{$store_name}";
            wp_register_style($key, false);
            wp_add_inline_style($key, $styles);
            wp_enqueue_style($key);
        }
    }
}

WordPress Version: 6.2

/**
 * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
 * Styles are stored via the style engine API.
 *
 * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
 *
 * @since 6.1.0
 *
 * @param array $options {
 *     Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context(). Default empty array.
 *
 *     @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
 *     @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined.
 * }
 *
 * @return void
 */
function wp_enqueue_stored_styles($options = array())
{
    $is_block_theme = wp_is_block_theme();
    $is_classic_theme = !$is_block_theme;
    /*
     * For block themes, this function prints stored styles in the header.
     * For classic themes, in the footer.
     */
    if ($is_block_theme && doing_action('wp_footer') || $is_classic_theme && doing_action('wp_enqueue_scripts')) {
        return;
    }
    $core_styles_keys = array('block-supports');
    $compiled_core_stylesheet = '';
    $style_tag_id = 'core';
    // Adds comment if code is prettified to identify core styles sections in debugging.
    $should_prettify = isset($options['prettify']) ? true === $options['prettify'] : (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
    foreach ($core_styles_keys as $style_key) {
        if ($should_prettify) {
            $compiled_core_stylesheet .= "/**\n * Core styles: {$style_key}\n */\n";
        }
        // Chains core store ids to signify what the styles contain.
        $style_tag_id .= '-' . $style_key;
        $compiled_core_stylesheet .= wp_style_engine_get_stylesheet_from_context($style_key, $options);
    }
    // Combines Core styles.
    if (!empty($compiled_core_stylesheet)) {
        wp_register_style($style_tag_id, false);
        wp_add_inline_style($style_tag_id, $compiled_core_stylesheet);
        wp_enqueue_style($style_tag_id);
    }
    // Prints out any other stores registered by themes or otherwise.
    $additional_stores = WP_Style_Engine_CSS_Rules_Store::get_stores();
    foreach (array_keys($additional_stores) as $store_name) {
        if (in_array($store_name, $core_styles_keys, true)) {
            continue;
        }
        $styles = wp_style_engine_get_stylesheet_from_context($store_name, $options);
        if (!empty($styles)) {
            $key = "wp-style-engine-{$store_name}";
            wp_register_style($key, false);
            wp_add_inline_style($key, $styles);
            wp_enqueue_style($key);
        }
    }
}

WordPress Version: 6.1

/**
 * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
 * Styles are stored via the style engine API.
 *
 * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
 *
 * @since 6.1.0
 *
 * @param array $options {
 *     Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context(). Default empty array.
 *
 *     @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
 *     @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined.
 * }
 *
 * @return void
 */
function wp_enqueue_stored_styles($options = array())
{
    $is_block_theme = wp_is_block_theme();
    $is_classic_theme = !$is_block_theme;
    /*
     * For block themes, this function prints stored styles in the header.
     * For classic themes, in the footer.
     */
    if ($is_block_theme && doing_action('wp_footer') || $is_classic_theme && doing_action('wp_enqueue_scripts')) {
        return;
    }
    $core_styles_keys = array('block-supports');
    $compiled_core_stylesheet = '';
    $style_tag_id = 'core';
    // Adds comment if code is prettified to identify core styles sections in debugging.
    $should_prettify = isset($options['prettify']) ? true === $options['prettify'] : (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
    foreach ($core_styles_keys as $style_key) {
        if ($should_prettify) {
            $compiled_core_stylesheet .= "/**\n * Core styles: {$style_key}\n */\n";
        }
        // Chains core store ids to signify what the styles contain.
        $style_tag_id .= '-' . $style_key;
        $compiled_core_stylesheet .= wp_style_engine_get_stylesheet_from_context($style_key, $options);
    }
    // Combines Core styles.
    if (!empty($compiled_core_stylesheet)) {
        wp_register_style($style_tag_id, false, array(), true, true);
        wp_add_inline_style($style_tag_id, $compiled_core_stylesheet);
        wp_enqueue_style($style_tag_id);
    }
    // Prints out any other stores registered by themes or otherwise.
    $additional_stores = WP_Style_Engine_CSS_Rules_Store::get_stores();
    foreach (array_keys($additional_stores) as $store_name) {
        if (in_array($store_name, $core_styles_keys, true)) {
            continue;
        }
        $styles = wp_style_engine_get_stylesheet_from_context($store_name, $options);
        if (!empty($styles)) {
            $key = "wp-style-engine-{$store_name}";
            wp_register_style($key, false, array(), true, true);
            wp_add_inline_style($key, $styles);
            wp_enqueue_style($key);
        }
    }
}