WP_Theme_JSON_Resolver::get_theme_data( array $deprecated = array(), array $options = array() ): WP_Theme_JSON

Returns the theme’s data.


Description

Data from theme.json will be backfilled from existing theme supports, if any. Note that if the same data is present in theme.json and in theme supports, the theme.json takes precedence.


Top ↑

Parameters

$deprecated array Optional
Deprecated. Not used.

Default: array()

$options array Optional
Options arguments.
  • with_supports bool
    Whether to include theme supports in the data. Default true.

Default: array()


Top ↑

Return

WP_Theme_JSON Entity that holds theme data.


Top ↑

Source

File: wp-includes/class-wp-theme-json-resolver.php. View all references

public static function get_theme_data( $deprecated = array(), $options = array() ) {
	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __METHOD__, '5.9.0' );
	}

	$options = wp_parse_args( $options, array( 'with_supports' => true ) );

	if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
		$wp_theme        = wp_get_theme();
		$theme_json_file = $wp_theme->get_file_path( 'theme.json' );
		if ( is_readable( $theme_json_file ) ) {
			$theme_json_data = static::read_json_file( $theme_json_file );
			$theme_json_data = static::translate( $theme_json_data, $wp_theme->get( 'TextDomain' ) );
		} else {
			$theme_json_data = array();
		}

		/**
		 * Filters the data provided by the theme for global styles and settings.
		 *
		 * @since 6.1.0
		 *
		 * @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
		 */
		$theme_json      = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
		$theme_json_data = $theme_json->get_data();
		static::$theme   = new WP_Theme_JSON( $theme_json_data );

		if ( $wp_theme->parent() ) {
			// Get parent theme.json.
			$parent_theme_json_file = $wp_theme->parent()->get_file_path( 'theme.json' );
			if ( $theme_json_file !== $parent_theme_json_file && is_readable( $parent_theme_json_file ) ) {
				$parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
				$parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) );
				$parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );

				/*
				 * Merge the child theme.json into the parent theme.json.
				 * The child theme takes precedence over the parent.
				 */
				$parent_theme->merge( static::$theme );
				static::$theme = $parent_theme;
			}
		}
	}

	if ( ! $options['with_supports'] ) {
		return static::$theme;
	}

	/*
	 * We want the presets and settings declared in theme.json
	 * to override the ones declared via theme supports.
	 * So we take theme supports, transform it to theme.json shape
	 * and merge the static::$theme upon that.
	 */
	$theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );
	if ( ! wp_theme_has_theme_json() ) {
		if ( ! isset( $theme_support_data['settings']['color'] ) ) {
			$theme_support_data['settings']['color'] = array();
		}

		$default_palette = false;
		if ( current_theme_supports( 'default-color-palette' ) ) {
			$default_palette = true;
		}
		if ( ! isset( $theme_support_data['settings']['color']['palette'] ) ) {
			// If the theme does not have any palette, we still want to show the core one.
			$default_palette = true;
		}
		$theme_support_data['settings']['color']['defaultPalette'] = $default_palette;

		$default_gradients = false;
		if ( current_theme_supports( 'default-gradient-presets' ) ) {
			$default_gradients = true;
		}
		if ( ! isset( $theme_support_data['settings']['color']['gradients'] ) ) {
			// If the theme does not have any gradients, we still want to show the core ones.
			$default_gradients = true;
		}
		$theme_support_data['settings']['color']['defaultGradients'] = $default_gradients;

		// Classic themes without a theme.json don't support global duotone.
		$theme_support_data['settings']['color']['defaultDuotone'] = false;

		// Allow themes to enable link color setting via theme_support.
		if ( current_theme_supports( 'link-color' ) ) {
			$theme_support_data['settings']['color']['link'] = true;
		}

		// Allow themes to enable all border settings via theme_support.
		if ( current_theme_supports( 'border' ) ) {
			$theme_support_data['settings']['border']['color']  = true;
			$theme_support_data['settings']['border']['radius'] = true;
			$theme_support_data['settings']['border']['style']  = true;
			$theme_support_data['settings']['border']['width']  = true;
		}
	}
	$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
	$with_theme_supports->merge( static::$theme );
	return $with_theme_supports;
}

Top ↑

Hooks



Top ↑

Changelog

Changelog
Version Description
6.0.0 Added an $options parameter to allow the theme data to be returned without theme supports.
5.9.0 Theme supports have been inlined and the $theme_support_data argument removed.
5.8.0 Introduced.

Top ↑

User Contributed Notes

You must log in before being able to contribute a note or feedback.