wp_get_global_settings()WP 5.9.0

Gets global settings by merging the values of core settings, theme, and user data.

I supplemented the descriptions with official documentation and examples.

The function caches the result, but if the theme development mode is enabled - wp_is_development_mode('theme'), the cache is not used.

No Hooks.

Returns

Mixed. An array of all settings or a specific value by $path.

Usage

wp_get_global_settings( $path, $context );
$path(array)

The path to a specific setting to retrieve. If empty, it will return all settings.

An array of keys to search for the desired value, for example ['color','gradients','theme'].

Default: array()

$context(array)

Additional context array. Metadata to know where to extract $path.

Default: array()

  • block_name(string)
    Determines which block to use for getting settings and allows retrieving settings for a specific block.

    If empty, it will return settings for the global context.

  • origin(string)
    Where to get the data from. If empty or unknown, 'all' is used. Allowed values:

    • all — core + theme + user.
    • base — only core + theme.

Examples

0

#1 Demo

$all = wp_get_global_settings();
print_r( $all );

We will get:

[
	'appearanceTools' => false,
	'useRootPaddingAwareAlignments' => true,
	'border' => ['color' => true, 'radius' => true, 'style' => true, 'width' => true],
	'color' => [
		'background' => true, 'button' => false, 'caption' => false, 'custom' => false, 'customDuotone' => false,
		'customGradient' => false, 'defaultDuotone' => true, 'defaultGradients' => true, 'defaultPalette' => true,
		'duotone' => ['default' => [...], 'theme' => []],
		'gradients' => ['default' => [...], 'theme' => [...]],
		'heading' => false, 'link' => true, 'palette' => ['default' => [...], 'theme' => [...]],
		'text' => true
	],
	'dimensions' => ['defaultAspectRatios' => true, 'aspectRatios' => ['default' => [...]]],
	'shadow' => ['defaultPresets' => true, 'presets' => ['default' => [...]]],
	'spacing' => [
		'blockGap' => true, 'margin' => true, 'padding' => true, 'customSpacingSize' => false,
		'defaultSpacingSizes' => false,
		'units' => ['%', 'em', 'rem', 'vw', 'vh', 'dvh', 'lvh', 'svh', 'vmin', 'vmax'],
		'spacingScale' => ['default' => ['operator' => '*', 'increment' => 1.5, 'steps' => 7, 'mediumStep' => 1.5, 'unit' => 'rem']],
		'spacingSizes' => ['default' => [...], 'theme' => [...]]
	],
	'typography' => [
		'customFontSize' => false, 'defaultFontSizes' => false, 'dropCap' => false,
		'fontSizes' => ['default' => [...], 'theme' => [...]],
		'fontStyle' => true, 'fontWeight' => true, 'letterSpacing' => false, 'lineHeight' => false,
		'textAlign' => true, 'textDecoration' => true, 'textTransform' => true, 'writingMode' => true, 'fluid' => false,
		'fontFamilies' => ['theme' => [...]]
	],
	'blocks' => [
		'core/button' => ['border' => ['radius' => true]],
		'core/image' => ['lightbox' => ['allowEditing' => true]],
		'core/pullquote' => ['border' => ['color' => true, 'radius' => true, 'style' => true, 'width' => true]]
	],
	'background' => ['backgroundImage' => true, 'backgroundSize' => true],
	'layout' => [
		'contentSize' => 'var(--layout-content-width)',
		'wideSize' => 'var(--layout-wide-content-width)',
		'allowCustomContentAndWideSize' => true,
		'allowEditing' => true
	]
]
0

#2 Getting gradients by theme

This example shows how to get the final value of the option (theme.json): settings.color.gradients.theme:

$grads = wp_get_global_settings( [ 'color','gradients','theme' ] );
print_r( $grads );

/*
Array(
	[0] => Array (
		[name] => surface-gradient
		[slug] => surface-gradient
		[gradient] => var(--color-surface-gradient)
	)
)
*/

Changelog

Since 5.9.0 Introduced.

wp_get_global_settings() code WP 6.8.1

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 );
}