WP_Theme_JSON::get_css_variables() WordPress Method

The WP_Theme_JSON::get_css_variables() method is used to get an array of CSS variables for the current theme. This can be useful for making dynamic changes to the theme based on user input or other factors.

WP_Theme_JSON::get_css_variables( array $nodes, array $origins ) #

Converts each styles section into a list of rulesets to be appended to the stylesheet.


Description

These rulesets contain all the css variables (custom variables and preset variables).

See glossary at https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax

For each section this creates a new ruleset such as:

block-selector {
  --wp--preset--category--slug: value;
  --wp--custom--variable: value;
}

Top ↑

Parameters

$nodes

(array)(Required)Nodes with settings.

$origins

(array)(Required)List of origins to process.


Top ↑

Return

(string) The new stylesheet.


Top ↑

Source

File: wp-includes/class-wp-theme-json.php

	protected function get_css_variables( $nodes, $origins ) {
		$stylesheet = '';
		foreach ( $nodes as $metadata ) {
			if ( null === $metadata['selector'] ) {
				continue;
			}

			$selector = $metadata['selector'];

			$node         = _wp_array_get( $this->theme_json, $metadata['path'], array() );
			$declarations = array_merge( static::compute_preset_vars( $node, $origins ), static::compute_theme_vars( $node ) );

			$stylesheet .= static::to_ruleset( $selector, $declarations );
		}

		return $stylesheet;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Added the $origins parameter.
5.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.