wp_get_global_styles

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

WordPress Version: 6.3

/**
 * Gets the styles resulting of merging core, theme, and user data.
 *
 * @since 5.9.0
 * @since 6.3.0 the internal link format "var:preset|color|secondary" is resolved
 *              to "var(--wp--preset--font-size--small)" so consumers don't have to.
 * @since 6.3.0 `transforms` is now usable in the `context` parameter. In case [`transforms`]['resolve_variables']
 *              is defined, variables are resolved to their value in the styles.
 *
 * @param array $path    Path to the specific style to retrieve. Optional.
 *                       If empty, will return all styles.
 * @param array $context {
 *     Metadata to know where to retrieve the $path from. Optional.
 *
 *     @type string $block_name Which block to retrieve the styles from.
 *                              If empty, it'll return the styles for the global context.
 *     @type string $origin     Which 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.
 *     @type array $transforms Which transformation(s) to apply.
 *                              Valid value is array( 'resolve-variables' ).
 *                              If defined, variables are resolved to their value in the styles.
 * }
 * @return mixed The styles array or individual style value to retrieve.
 */
function wp_get_global_styles($path = array(), $context = array())
{
    if (!empty($context['block_name'])) {
        $path = array_merge(array('blocks', $context['block_name']), $path);
    }
    $origin = 'custom';
    if (isset($context['origin']) && 'base' === $context['origin']) {
        $origin = 'theme';
    }
    $resolve_variables = isset($context['transforms']) && is_array($context['transforms']) && in_array('resolve-variables', $context['transforms'], true);
    $merged_data = WP_Theme_JSON_Resolver::get_merged_data($origin);
    if ($resolve_variables) {
        $merged_data = WP_Theme_JSON::resolve_variables($merged_data);
    }
    $styles = $merged_data->get_raw_data()['styles'];
    return _wp_array_get($styles, $path, $styles);
}

WordPress Version: 6.1

/**
 * Gets the styles resulting of merging core, theme, and user data.
 *
 * @since 5.9.0
 *
 * @param array $path    Path to the specific style to retrieve. Optional.
 *                       If empty, will return all styles.
 * @param array $context {
 *     Metadata to know where to retrieve the $path from. Optional.
 *
 *     @type string $block_name Which block to retrieve the styles from.
 *                              If empty, it'll return the styles for the global context.
 *     @type string $origin     Which 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.
 * }
 * @return array The styles to retrieve.
 */
function wp_get_global_styles($path = array(), $context = array())
{
    if (!empty($context['block_name'])) {
        $path = array_merge(array('blocks', $context['block_name']), $path);
    }
    $origin = 'custom';
    if (isset($context['origin']) && 'base' === $context['origin']) {
        $origin = 'theme';
    }
    $styles = WP_Theme_JSON_Resolver::get_merged_data($origin)->get_raw_data()['styles'];
    return _wp_array_get($styles, $path, $styles);
}

WordPress Version: 5.9

/**
 * Function to get the styles resulting of merging core, theme, and user data.
 *
 * @since 5.9.0
 *
 * @param array $path    Path to the specific style to retrieve. Optional.
 *                       If empty, will return all styles.
 * @param array $context {
 *     Metadata to know where to retrieve the $path from. Optional.
 *
 *     @type string $block_name Which block to retrieve the styles from.
 *                              If empty, it'll return the styles for the global context.
 *     @type string $origin     Which 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.
 * }
 *
 * @return array The styles to retrieve.
 */
function wp_get_global_styles($path = array(), $context = array())
{
    if (!empty($context['block_name'])) {
        $path = array_merge(array('blocks', $context['block_name']), $path);
    }
    $origin = 'custom';
    if (isset($context['origin']) && 'base' === $context['origin']) {
        $origin = 'theme';
    }
    $styles = WP_Theme_JSON_Resolver::get_merged_data($origin)->get_raw_data()['styles'];
    return _wp_array_get($styles, $path, $styles);
}