Gets the settings resulting of merging core, theme, and user data.
Parameters
$path
arrayoptional- Path to the specific setting to retrieve. Optional.
If empty, will return all settings.Default:
array()
$context
arrayoptional- Metadata to know where to retrieve the $path from. Optional.
block_name
stringWhich block to retrieve the settings from.
If empty, it’ll return the settings for the global context.origin
stringWhich origin to take data from.
Valid values are'all'
(core, theme, and user) or'base'
(core and theme).
If empty or unknown,'all'
is used.
Default:
array()
Source
function wp_get_global_settings( $path = array(), $context = array() ) {
if ( ! empty( $context['block_name'] ) ) {
$new_path = array( 'blocks', $context['block_name'] );
foreach ( $path as $subpath ) {
$new_path[] = $subpath;
}
$path = $new_path;
}
/*
* This is the default value when no origin is provided or when it is 'all'.
*
* The $origin is used as part of the cache key. Changes here need to account
* for clearing the cache appropriately.
*/
$origin = 'custom';
if (
! wp_theme_has_theme_json() ||
( isset( $context['origin'] ) && 'base' === $context['origin'] )
) {
$origin = 'theme';
}
/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places.
*
* The rationale for this is to make sure derived data from theme.json
* is always fresh from the potential modifications done via hooks
* that can use dynamic data (modify the stylesheet depending on some option,
* settings depending on user permissions, etc.).
* See some of the existing hooks to modify theme.json behavior:
* https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* A different alternative considered was to invalidate the cache upon certain
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* See https://github.com/WordPress/gutenberg/pull/45372
*/
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_settings_' . $origin;
/*
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = ! wp_is_development_mode( 'theme' );
$settings = false;
if ( $can_use_cached ) {
$settings = wp_cache_get( $cache_key, $cache_group );
}
if ( false === $settings ) {
$settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $settings, $cache_group );
}
}
return _wp_array_get( $settings, $path, $settings );
}
Changelog
Version | Description |
---|---|
5.9.0 | Introduced. |
To know how to use the
$path
argument withwp_get_global_settings()
to retrieve a specific setting, you can try it without first.This gives you a formatted array of every available settings, from this, you can locate the setting you’d like to retrieve.
For example, gradients are located in
$settings['color']['gradients']['theme']
Take the
$settings
path, here it’s :['color']['gradients']['theme']
, and transform that in a flat array like this :['color','gradients','theme']
, this is the value you need to pass as the$path
argument withwp_get_global_settings($path)
.Example 1, if you’d like to retrieve gradients specified in the theme.json for example, you could use the path argument like so :
Example 2, to retrieve the spacing scale :