WP_Theme_JSON::get_style_nodes() WordPress Method

This method returns an array of style nodes for the current theme.

WP_Theme_JSON::get_style_nodes( array $theme_json, array $selectors = array() ) #

Builds metadata for the style nodes, which returns in the form of:


Description

[ [ ‘path’ => [ ‘path’, ‘to’, ‘some’, ‘node’ ], ‘selector’ => ‘CSS selector for some node’, ‘duotone’ => ‘CSS selector for duotone for some node’ ], [ ‘path’ => [‘path’, ‘to’, ‘other’, ‘node’ ], ‘selector’ => ‘CSS selector for other node’, ‘duotone’ => null ], ]


Top ↑

Parameters

$theme_json

(array)(Required)The tree to extract style nodes from.

$selectors

(array)(Optional)List of selectors per block.

Default value: array()


Top ↑

Return

(array)


Top ↑

Source

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

	protected static function get_style_nodes( $theme_json, $selectors = array() ) {
		$nodes = array();
		if ( ! isset( $theme_json['styles'] ) ) {
			return $nodes;
		}

		// Top-level.
		$nodes[] = array(
			'path'     => array( 'styles' ),
			'selector' => static::ROOT_BLOCK_SELECTOR,
		);

		if ( isset( $theme_json['styles']['elements'] ) ) {
			foreach ( $theme_json['styles']['elements'] as $element => $node ) {
				$nodes[] = array(
					'path'     => array( 'styles', 'elements', $element ),
					'selector' => static::ELEMENTS[ $element ],
				);
			}
		}

		// Blocks.
		if ( ! isset( $theme_json['styles']['blocks'] ) ) {
			return $nodes;
		}

		foreach ( $theme_json['styles']['blocks'] as $name => $node ) {
			$selector = null;
			if ( isset( $selectors[ $name ]['selector'] ) ) {
				$selector = $selectors[ $name ]['selector'];
			}

			$duotone_selector = null;
			if ( isset( $selectors[ $name ]['duotone'] ) ) {
				$duotone_selector = $selectors[ $name ]['duotone'];
			}

			$nodes[] = array(
				'path'     => array( 'styles', 'blocks', $name ),
				'selector' => $selector,
				'duotone'  => $duotone_selector,
			);

			if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) {
				foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) {
					$nodes[] = array(
						'path'     => array( 'styles', 'blocks', $name, 'elements', $element ),
						'selector' => $selectors[ $name ]['elements'][ $element ],
					);
				}
			}
		}

		return $nodes;
	}

Top ↑

Changelog

Changelog
VersionDescription
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.