Function and usage of get_theme_root() in wordpress


Answers ( 1 )


    The get_theme_root() function in WordPress is used to retrieve the path to the themes directory. Here's a detailed explanation of its usage:

    Function Signature:

    get_theme_root( string $stylesheet_or_template = '' ): string


    • Purpose: This function is designed to fetch the file system path to the directory where WordPress themes are stored.
    • Trailing Slash: It's important to note that the returned path does not include a trailing slash.


    1. $stylesheet_or_template (string, optional):
      • This parameter specifies the stylesheet or template name of the theme for which the path is being retrieved.
      • It's an optional parameter. If not provided, the function defaults to the main theme root.
      • Default Value: '' (an empty string).

    Return Value:

    • Type: string
    • Description: The function returns the path to the themes directory as a string.

    Usage Example:

    Sample Code:

    // Example 1: Get the root directory for the current theme
    $current_theme_root = get_theme_root();
    // Example 2: Get the root directory for a specific theme by its stylesheet name
    $specific_theme_root = get_theme_root('twentytwenty');
    // Output the results
    echo "Current Theme Root: " . $current_theme_root;
    echo "Specific Theme Root: " . $specific_theme_root;


    • Example 1: Retrieves the path to the root directory of the currently active theme.
    • Example 2: Fetches the path to the root directory for a specific theme, in this case, 'twentytwenty'.

    Additional Notes:

    • This function is useful when you need to programmatically access the themes directory, for example, to include theme-specific files or to check for the existence of certain theme files.
    • It's a part of WordPress's theme-related functionality and is often used in theme development and customization.

    Remember that this function only provides the path; it does not check if the theme or file actually exists at that path.

Leave an answer