wp_enqueue_global_styles

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

WordPress Version: 6.2

/**
 * Enqueues the global styles defined via theme.json.
 *
 * @since 5.8.0
 */
function wp_enqueue_global_styles()
{
    $separate_assets = wp_should_load_separate_core_block_assets();
    $is_block_theme = wp_is_block_theme();
    $is_classic_theme = !$is_block_theme;
    /*
     * Global styles should be printed in the head when loading all styles combined.
     * The footer should only be used to print global styles for classic themes with separate core assets enabled.
     *
     * See https://core.trac.wordpress.org/ticket/53494.
     */
    if ($is_block_theme && doing_action('wp_footer') || $is_classic_theme && doing_action('wp_footer') && !$separate_assets || $is_classic_theme && doing_action('wp_enqueue_scripts') && $separate_assets) {
        return;
    }
    /*
     * If loading the CSS for each block separately, then load the theme.json CSS conditionally.
     * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
     * This filter must be registered before calling wp_get_global_stylesheet();
     */
    add_filter('wp_theme_json_get_style_nodes', 'wp_filter_out_block_nodes');
    $stylesheet = wp_get_global_stylesheet();
    if (empty($stylesheet)) {
        return;
    }
    wp_register_style('global-styles', false);
    wp_add_inline_style('global-styles', $stylesheet);
    wp_enqueue_style('global-styles');
    // Add each block as an inline css.
    wp_add_global_styles_for_blocks();
}

WordPress Version: 6.1

/**
 * Enqueues the global styles defined via theme.json.
 *
 * @since 5.8.0
 */
function wp_enqueue_global_styles()
{
    $separate_assets = wp_should_load_separate_core_block_assets();
    $is_block_theme = wp_is_block_theme();
    $is_classic_theme = !$is_block_theme;
    /*
     * Global styles should be printed in the head when loading all styles combined.
     * The footer should only be used to print global styles for classic themes with separate core assets enabled.
     *
     * See https://core.trac.wordpress.org/ticket/53494.
     */
    if ($is_block_theme && doing_action('wp_footer') || $is_classic_theme && doing_action('wp_footer') && !$separate_assets || $is_classic_theme && doing_action('wp_enqueue_scripts') && $separate_assets) {
        return;
    }
    /*
     * If loading the CSS for each block separately, then load the theme.json CSS conditionally.
     * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
     * This filter must be registered before calling wp_get_global_stylesheet();
     */
    add_filter('wp_theme_json_get_style_nodes', 'wp_filter_out_block_nodes');
    $stylesheet = wp_get_global_stylesheet();
    if (empty($stylesheet)) {
        return;
    }
    wp_register_style('global-styles', false, array(), true, true);
    wp_add_inline_style('global-styles', $stylesheet);
    wp_enqueue_style('global-styles');
    // Add each block as an inline css.
    wp_add_global_styles_for_blocks();
}

WordPress Version: 9.1

/**
 * Enqueues the global styles defined via theme.json.
 *
 * @since 5.8.0
 */
function wp_enqueue_global_styles()
{
    $separate_assets = wp_should_load_separate_core_block_assets();
    $is_block_theme = wp_is_block_theme();
    $is_classic_theme = !$is_block_theme;
    /*
     * Global styles should be printed in the head when loading all styles combined.
     * The footer should only be used to print global styles for classic themes with separate core assets enabled.
     *
     * See https://core.trac.wordpress.org/ticket/53494.
     */
    if ($is_block_theme && doing_action('wp_footer') || $is_classic_theme && doing_action('wp_footer') && !$separate_assets || $is_classic_theme && doing_action('wp_enqueue_scripts') && $separate_assets) {
        return;
    }
    $stylesheet = wp_get_global_stylesheet();
    if (empty($stylesheet)) {
        return;
    }
    wp_register_style('global-styles', false, array(), true, true);
    wp_add_inline_style('global-styles', $stylesheet);
    wp_enqueue_style('global-styles');
}

WordPress Version: 5.9

/**
 * Enqueues the global styles defined via theme.json.
 *
 * @since 5.8.0
 */
function wp_enqueue_global_styles()
{
    $separate_assets = wp_should_load_separate_core_block_assets();
    /*
     * Global styles should be printed in the head when loading all styles combined.
     * The footer should only be used to print global styles for classic themes with separate core assets enabled.
     *
     * See https://core.trac.wordpress.org/ticket/53494.
     */
    if (!$separate_assets && doing_action('wp_footer') || $separate_assets && doing_action('wp_enqueue_scripts')) {
        return;
    }
    $stylesheet = wp_get_global_stylesheet();
    if (empty($stylesheet)) {
        return;
    }
    wp_register_style('global-styles', false, array(), true, true);
    wp_add_inline_style('global-styles', $stylesheet);
    wp_enqueue_style('global-styles');
}

WordPress Version: 5.8

/**
 * Enqueues the global styles defined via theme.json.
 *
 * @since 5.8.0
 */
function wp_enqueue_global_styles()
{
    if (!WP_Theme_JSON_Resolver::theme_has_support()) {
        return;
    }
    $separate_assets = wp_should_load_separate_core_block_assets();
    /*
     * Global styles should be printed in the head when loading all styles combined.
     * The footer should only be used to print global styles for classic themes with separate core assets enabled.
     *
     * See https://core.trac.wordpress.org/ticket/53494.
     */
    if (!$separate_assets && doing_action('wp_footer') || $separate_assets && doing_action('wp_enqueue_scripts')) {
        return;
    }
    $can_use_cache = (!defined('WP_DEBUG') || !WP_DEBUG) && (!defined('SCRIPT_DEBUG') || !SCRIPT_DEBUG) && (!defined('REST_REQUEST') || !REST_REQUEST) && !is_admin();
    $stylesheet = null;
    if ($can_use_cache) {
        $cache = get_transient('global_styles');
        if ($cache) {
            $stylesheet = $cache;
        }
    }
    if (null === $stylesheet) {
        $settings = get_default_block_editor_settings();
        $theme_json = WP_Theme_JSON_Resolver::get_merged_data($settings);
        $stylesheet = $theme_json->get_stylesheet();
        if ($can_use_cache) {
            set_transient('global_styles', $stylesheet, MINUTE_IN_SECONDS);
        }
    }
    if (empty($stylesheet)) {
        return;
    }
    wp_register_style('global-styles', false, array(), true, true);
    wp_add_inline_style('global-styles', $stylesheet);
    wp_enqueue_style('global-styles');
}