Using WordPress ‘get_theme_roots()’ PHP function

The get_theme_roots() WordPress PHP function retrieves the theme roots.

Usage

To use the function, simply call it and store the result in a variable or output it directly. For example:

$theme_roots = get_theme_roots();
echo $theme_roots;

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: get_theme_roots()

Examples

Displaying Theme Roots

This example retrieves the theme roots and displays it on the screen:

$theme_roots = get_theme_roots();
echo "Theme roots: " . $theme_roots;

Conditional Theme Roots Check

This example checks if the theme roots is a specific path and displays a custom message accordingly:

$theme_roots = get_theme_roots();

if ($theme_roots === '/themes') {
    echo "The default theme roots is being used.";
} else {
    echo "A custom theme roots is being used.";
}

Displaying Theme Directory

This example combines the theme roots with the content directory to display the full theme directory:

$theme_roots = get_theme_roots();
$content_dir = WP_CONTENT_DIR;
$theme_dir = $content_dir . $theme_roots;

echo "Theme Directory: " . $theme_dir;

List Themes in Theme Roots

This example lists all the themes available in the theme roots:

$theme_roots = get_theme_roots();
$themes_dir = WP_CONTENT_DIR . $theme_roots;
$themes = scandir($themes_dir);

foreach ($themes as $theme) {
    if ($theme !== '.' && $theme !== '..') {
        echo "Theme: " . $theme . "<br>";
    }
}

Count Themes in Theme Roots

This example counts the number of themes available in the theme roots:

$theme_roots = get_theme_roots();
$themes_dir = WP_CONTENT_DIR . $theme_roots;
$themes = array_diff(scandir($themes_dir), ['.', '..']);
$theme_count = count($themes);

echo "Number of themes: " . $theme_count;