The get_theme_root() WordPress PHP function retrieves the path to the themes directory without a trailing slash.
Usage
$theme_root = get_theme_root( $stylesheet_or_template );
Parameters
$stylesheet_or_template (string)
: Optional. The stylesheet or template name of the theme. Default is to leverage the main theme root. Default: ”.
More information
See WordPress Developer Resources: get_theme_root()
Examples
Display theme root path
This example displays the theme root path on the screen.
$theme_root = get_theme_root(); echo "The theme root path is: " . $theme_root;
List all theme directories
This example lists all theme directories inside the theme root.
$theme_root = get_theme_root(); $theme_directories = glob("$theme_root/*", GLOB_ONLYDIR); foreach ($theme_directories as $dir) { echo basename($dir) . "<br>"; }
Count subdirectories in themes directory
This example counts the 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 "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.
Check if a specific theme exists
This example checks if a specific theme exists in the themes directory.
function check_if_theme_exists($theme_name) { $theme_root = get_theme_root(); $theme_path = $theme_root . '/' . $theme_name; return is_dir($theme_path); } if (check_if_theme_exists('twentytwentyone')) { echo 'The theme exists.'; } else { echo 'The theme does not exist.'; }
Create a new theme directory
This example creates a new theme directory inside the theme root.
function create_theme_directory($new_theme_name) { $theme_root = get_theme_root(); $new_theme_path = $theme_root . '/' . $new_theme_name; if (!is_dir($new_theme_path)) { mkdir($new_theme_path); echo "New theme directory created: " . $new_theme_path; } else { echo "Theme directory already exists: " . $new_theme_path; } } create_theme_directory('my_custom_theme');