wppaste
WordPress

WP_Theme_JSON::get_property_value( array $styles, array $path, array $theme_json = null ): string|array

Since
5.8.0, 5.9.0, 6.1.0, 6.3.0, 6.7.0
Source
wp-includes/class-wp-theme-json.php:2520
Returns the style property for the given path.

Description

It also converts references to a path to the value stored at that location, e.g.
{ "ref": "style.color.background" } => "#fff".

Compatibility

WordPress
since 6.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$stylesarray
Styles subtree.
$patharray
Which property to process.
$theme_jsonarrayoptional
Theme JSON array.Default: null

Return value

string|array
Style property value.

Performance profile

How much work a call to WP_Theme_JSON::get_property_value() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–68

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 69.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • serializeserialisationjson_encode()called directly
  • hookthird-party callbacksdo_action()one call below WP_Theme_JSON::get_property_value()

Further down the call graph this can also reach query, option, cache and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme_JSON::get_property_value() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always12–18_wp_array_get()
$value !== "" && $value !== null && is_array($value) && isset($value)36–44_wp_array_get(), explode(), _wp_array_get()
$value !== "" && $value !== null && is_array($value) && isset($value) && is_array($ref_value) && isset($ref_value)62–68_wp_array_get(), explode(), _wp_array_get(), json_encode(), json_encode(), __(), sprintf(), _doing_it_wrong()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 69 instructions, 12–68 executed per call, 10 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 3

Source code

	protected static function get_property_value( $styles, $path, $theme_json = null ) {		$value = _wp_array_get( $styles, $path, '' ); 		if ( '' === $value || null === $value ) {			// No need to process the value further.			return '';		} 		/*		 * This converts references to a path to the value at that path		 * where the value is an array with a "ref" key, pointing to a path.		 * For example: { "ref": "style.color.background" } => "#fff".		 * In the case of backgroundImage, if both a ref and a URL are present in the value,		 * the URL takes precedence and the ref is ignored.		 */		if ( is_array( $value ) && isset( $value['ref'] ) ) {			$value_path = explode( '.', $value['ref'] );			$ref_value  = _wp_array_get( $theme_json, $value_path );			// Background Image refs can refer to a string or an array containing a URL string.			$ref_value_url = $ref_value['url'] ?? null;			// Only use the ref value if we find anything.			if ( ! empty( $ref_value ) && ( is_string( $ref_value ) || is_string( $ref_value_url ) ) ) {				$value = $ref_value;			} 			if ( is_array( $ref_value ) && isset( $ref_value['ref'] ) ) {				$path_string      = json_encode( $path );				$ref_value_string = json_encode( $ref_value );				_doing_it_wrong(					'get_property_value',					sprintf(						/* translators: 1: theme.json, 2: Value name, 3: Value path, 4: Another value name. */						__( 'Your %1$s file uses a dynamic value (%2$s) for the path at %3$s. However, the value at %3$s is also a dynamic value (pointing to %4$s) and pointing to another dynamic value is not supported. Please update %3$s to point directly to %4$s.' ),						'theme.json',						$ref_value_string,						$path_string,						$ref_value['ref']					),					'6.1.0'				);			}		} 		return $value;	}

Changelog

Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.7.0
Added support for background image refs.from the docblock
6.3.0
It no longer converts the internal format "var:preset|color|secondary" to the standard form "--wp--preset--color--secondary".
This is already done by the sanitize method, so every property will be in the standard form.from the docblock
6.1.0
Added the $theme_json parameter.from the docblock
5.9.0
Added support for values of array type, which are returned as is.from the docblock
5.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/class-wp-theme-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.