wp_theme_has_theme_json()WP 6.2.0

Checks whether the theme.json file exists in the active theme or in its parent theme.

The function helps determine whether the theme uses Global Styles settings via theme.json. This can be useful when you need to load editor styles, settings, or compatibility differently for classic and block themes.

First, the child theme directory is checked. If theme.json is not there, the parent theme directory is checked.

The file path is passed through the filter theme_file_path, so it can be modified. The result is cached for the current theme, but the cache is not used if the theme development mode is enabled — wp_is_development_mode().

Hooks from the function

Returns

true|false.

  • true — if the theme.json file is found in the active theme or the parent theme.
  • false — if the file is not found.

Usage

wp_theme_has_theme_json();

Examples

0

#1 Function to check for the presence of a theme.json file in the current theme or its parent theme

/**
 * Checks whether a theme or its parent has a theme.json file.
 *
 * @since 6.2.0
 *
 * @param WP_Theme $theme Theme object.
 *
 * @return bool True if theme or its parent has a theme.json file, false otherwise.
 */
function wp_theme_has_theme_json( $theme ) {
	$theme_json_file = $theme->get_stylesheet_directory() . '/theme.json';
	if ( file_exists( $theme_json_file ) ) {
		return true;
	}

	if ( $theme->parent() ) {
		$parent_theme = wp_get_theme( $theme->parent() );
		if ( $parent_theme && $parent_theme->exists() ) {
			return wp_theme_has_theme_json( $parent_theme );
		}
	}

	return false;
}
0

#2 Checking support for theme.json

Let's check whether the current theme has a file theme.json.

if ( wp_theme_has_theme_json() ) {
	echo 'The theme uses theme.json.';
} else {
	echo 'theme.json file not found.';
}
0

#3 Connecting styles only for themes without theme.json

The example shows how to load additional CSS only for a theme that doesn’t have theme.json.

add_action( 'wp_enqueue_scripts', 'prefix_enqueue_classic_theme_styles' );

function prefix_enqueue_classic_theme_styles() {
	if ( wp_theme_has_theme_json() ) {
		return;
	}

	wp_enqueue_style(
		'prefix-classic-theme',
		get_stylesheet_directory_uri() . '/classic-theme.css',
		[],
		'1.0.0'
	);
}
0

#4 Admin check

You can use a function to display an admin notification if the theme does not contain theme.json.

add_action( 'admin_notices', 'prefix_theme_json_admin_notice' );

function prefix_theme_json_admin_notice() {
	if ( wp_theme_has_theme_json() ) {
		return;
	}

	?>
	<div class="notice notice-info">
		<p><?php esc_html_e( 'A theme.json file was not found in the current theme.', 'textdomain' ); ?></p>
	</div>
	<?php
}
0

#5 Changing the path to theme.json via a filter

The theme_file_path filter allows you to change the path to the file that will be checked.

add_filter( 'theme_file_path', 'prefix_change_theme_json_path', 10, 2 );

function prefix_change_theme_json_path( $path, $file ) {
	if ( 'theme.json' !== $file ) {
		return $path;
	}

	return get_stylesheet_directory() . '/config/theme.json';
}

Changelog

Since 6.2.0 Introduced.

wp_theme_has_theme_json() code WP 7.0.1

function wp_theme_has_theme_json() {
	static $theme_has_support = array();

	$stylesheet = get_stylesheet();

	if (
		isset( $theme_has_support[ $stylesheet ] ) &&
		/*
		 * Ignore static cache when the development mode is set to 'theme', to avoid interfering with
		 * the theme developer's workflow.
		 */
		! wp_is_development_mode( 'theme' )
	) {
		return $theme_has_support[ $stylesheet ];
	}

	$stylesheet_directory = get_stylesheet_directory();
	$template_directory   = get_template_directory();

	// This is the same as get_theme_file_path(), which isn't available in load-styles.php context
	if ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/theme.json' ) ) {
		$path = $stylesheet_directory . '/theme.json';
	} else {
		$path = $template_directory . '/theme.json';
	}

	/** This filter is documented in wp-includes/link-template.php */
	$path = apply_filters( 'theme_file_path', $path, 'theme.json' );

	$theme_has_support[ $stylesheet ] = file_exists( $path );

	return $theme_has_support[ $stylesheet ];
}