get_theme_root()WP 1.5.0

Gets the system path of the directory where all themes are located. The slash at the end is absent.

Returns the path, not the URL: /home/example.com/public_html/wp-content/themes.

Used within WordPress when loading themes, files, and templates.

To get the URL to the themes directory, use get_theme_root_uri().

The result can be overridden via the filter theme_root.

1 time — 0.00001 sec (speed of light) | 50000 times — 0.01 sec (speed of light) | PHP 7.4.8, WP 5.6.2
Hooks from the function

Returns

String. Path to the templates directory.

Usage

$path = get_theme_root( $stylesheet_or_template );
$stylesheet_or_template(string)

The name of the theme or the theme styles, the parent folder of which needs to be obtained.

In 99% of cases, this parameter is useless. It is needed for cases when multiple folders are specified for themes and we need to get the parent folder of all themes, in which the theme specified in this parameter is located. See global $wp_theme_directories.
Default: ''

Examples

0

#1 Get the full path to the themes directory:

echo get_theme_root();             //> /home/k/foo/example.com/www/wp-content/themes
echo get_theme_root( 'my-theme' ); //> /home/k/foo/example.com/www/wp-content/themes
0

#2 Number of subdirectories in the themes directory

function display_themes_subdirs_count_info(){

  $theme_root = get_theme_root();
  $files_array = glob( "$theme_root/*", GLOB_ONLYDIR );

  echo count( $files_array ) . " subdirectories in the directory: $theme_root";
}

Notes

  • Global. String[]. $wp_theme_directories

Changelog

Since 1.5.0 Introduced.

get_theme_root() code WP 6.8.3

function get_theme_root( $stylesheet_or_template = '' ) {
	global $wp_theme_directories;

	$theme_root = '';

	if ( $stylesheet_or_template ) {
		$theme_root = get_raw_theme_root( $stylesheet_or_template );
		if ( $theme_root ) {
			/*
			 * Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
			 * This gives relative theme roots the benefit of the doubt when things go haywire.
			 */
			if ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
				$theme_root = WP_CONTENT_DIR . $theme_root;
			}
		}
	}

	if ( ! $theme_root ) {
		$theme_root = WP_CONTENT_DIR . '/themes';
	}

	/**
	 * Filters the absolute path to the themes directory.
	 *
	 * @since 1.5.0
	 *
	 * @param string $theme_root Absolute path to themes directory.
	 */
	return apply_filters( 'theme_root', $theme_root );
}