wp_get_global_stylesheet()
Gets the ready CSS code from the merged data of theme.json, style variations, and custom user settings.
The function collects presets (palettes, scales, borders, etc.), variables, and other declarations from theme.json, takes into account custom modifications in the "Global Styles" panel, and then turns them into a CSS string.
The result is cached in object-cache (group theme_json) and is not cached when dev mode theme is enabled.
By default, global variables will be included via wp_enqueue_global_styles_css_custom_properties() on the hook:
// add wp_get_global_stylesheet( [ 'variables' ] ) add_action( 'enqueue_block_editor_assets', 'wp_enqueue_global_styles_css_custom_properties' );
Use wp_clean_theme_json_cache() to clear the cache if you need to immediately get updated styles.
No Hooks.
Returns
String. CSS string of the global styles of the theme.
Usage
wp_get_global_stylesheet( $types );
- $types(array)
An array of segments to include in the output. By passing the required segments, you can reduce the volume of styles if only part is needed.
Possible segments:
- variables — only CSS variables (Custom Properties) for presets and custom values.
- styles — only the content of the
stylessection intheme.json. - presets — only CSS classes for presets.
- base-layout-styles — only base layout styles.
- custom-css — only custom CSS.
If an empty array is specified (by default), the following segments will be obtained:
- For themes without theme.json:
variables,presets,base-layout-styles. - For themes with theme.json:
variables,presets,styles.
Allowed values match the first argument of the method WP_Theme_JSON::get_stylesheet().
By default:
[]
Examples
#1 Demo
The result depends on the parameters of theme.json and style settings.
variables:
echo wp_get_global_stylesheet( [ 'variables' ] );
:root {
--wp--preset--aspect-ratio--square: 1;
--wp--preset--aspect-ratio--4-3: 4/3;
--wp--preset--color--white: #ffffff;
--wp--preset--color--light-green-cyan: #7bdcb5;
--wp--preset--color--vivid-green-cyan: #00d084;
--wp--preset--font-size--small: 13px;
--wp--preset--font-size--medium: 20px;
--wp--preset--spacing--20: 0.44rem;
--wp--preset--spacing--30: 0.67rem;
//...
}
presets:
echo wp_get_global_stylesheet( [ 'presets' ] );
.has-black-color {
color: var(--wp--preset--color--black) !important;
}
.has-cyan-bluish-gray-color {
color: var(--wp--preset--color--cyan-bluish-gray) !important;
}
//...
styles:
echo wp_get_global_stylesheet( [ 'styles' ] );
Changelog
| Since 5.9.0 | Introduced. |
| Since 6.1.0 | Added 'base-layout-styles' support. |
| Since 6.6.0 | Resolves relative paths in theme.json styles to theme absolute paths. |
| Since 7.0.0 | Deprecated 'base-layout-styles' type; classic themes now receive full styles with layout-specific alignment rules skipped via base_layout_styles option. |