Function and usage of get_theme_roots() in wordpress


Answers ( 1 )


    The get_theme_roots() function in WordPress is used to retrieve the roots of themes installed in a WordPress environment. This function is particularly useful in situations where you have multiple themes installed, possibly in different directories, and you need to know their root paths.

    Function Signature:

    get_theme_roots(): array|string

    Function Behavior:

    • Purpose: The primary function of get_theme_roots() is to return the root directories of the installed themes. These roots are where the theme's files are located.
    • Return Value: It returns either an array or a string, depending on the setup of your themes.
      • Array: If your WordPress installation contains themes in different root directories, get_theme_roots() returns an array. Each element in this array is keyed by the theme's template or stylesheet name, and the value is the corresponding root directory.
      • String: If all themes are located in the same root directory, get_theme_roots() simplifies the return value to a single string representing that common root directory.

    Sample Usage:

    Example 1: Retrieve Roots of All Themes

    Suppose you have multiple themes installed in different directories and you want to list their root paths. Here's how you can use get_theme_roots():

    $theme_roots = get_theme_roots();
    if (is_array($theme_roots)) {
        foreach ($theme_roots as $theme => $root) {
            echo "Theme: {$theme}, Root: {$root}<br>";
    } else {
        echo "All themes share the same root: {$theme_roots}";

    Example 2: Checking for a Common Root

    In a case where you want to check if all themes are in the same root directory:

    $theme_roots = get_theme_roots();
    if (is_string($theme_roots)) {
        echo "All themes are located in: {$theme_roots}";
    } else {
        echo "Themes are in different roots.";

    Use Cases:

    • Theme Management: Useful for developers managing multiple themes, especially in complex WordPress installations.
    • Diagnostic Tool: Helps in debugging and ensuring that themes are correctly installed in their respective directories.
    • Custom Plugin Development: When creating plugins that interact with themes, this function provides necessary directory information.

    Remember, get_theme_roots() is a low-level function and is more commonly used in plugin development or advanced theme management scenarios rather than in typical theme or site setup.

Leave an answer