get_theme_root( string $stylesheet_or_template = '' )

Retrieve path to themes directory.


Description Description

Does not have trailing slash.


Parameters Parameters

$stylesheet_or_template

(string) (Optional) The stylesheet or template name of the theme. Default is to leverage the main theme root.

Default value: ''


Top ↑

Return Return

(string) Themes directory path.


Top ↑

Source Source

File: wp-includes/theme.php

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 ) ) {
				$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 );
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Number of Subdirectories in Themes Directory
    The function below informs about the number of subdirectories in the themes directory. Note that this doesn’t necessarily match the number of themes recognized by WordPress.

    <?php
    function display_themes_subdirs_count_info()
      $theme_root = get_theme_root();
      $files_array = glob("$theme_root/*", GLOB_ONLYDIR);
      echo "There are " . count($files_array) . " subdirectories in the " . $theme_root . " directory"; 
    }
    ?>
    

    Example output:

    There are 5 subdirectories in the /home/user/public_html/wp-content/themes directory.

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