Retrieving the root URI of a theme in WordPress with get_theme_root_uri

The get_theme_root_uri function in WordPress returns the URI of the current theme’s root directory. This can be useful for obtaining the URL of the theme’s directory in order to enqueue stylesheets, scripts, or other assets from the theme’s directory. It provides a convenient way to reference theme assets without hardcoding the theme directory path.

Parameters accepted by get_theme_root_uri function

The get_theme_root_uri function accepts the following parameters:

  • $stylesheet_or_template (string, optional. Default value: ”) – The stylesheet or template name of the theme. Default is to leverage the main theme root.
  • $theme_root (string, optional. Default value: ”) – The theme root for which calculations will be based, preventing the need for a get_raw_theme_root() call.

Value returned by get_theme_root_uri function

The get_theme_root_uri function returns a string representing the themes directory URI.

Examples

How to get the theme root URI

The get_theme_root_uri function is commonly used to retrieve the URI of the current theme’s root directory.

$theme_root_uri = get_theme_root_uri();
echo $theme_root_uri;

This code snippet retrieves the URI of the current theme’s root directory using the get_theme_root_uri function and then echoes the result.

How to get the theme root URI for a specific theme

It is also possible to retrieve the URI of a specific theme’s root directory using the get_theme_root_uri function.

$theme_name = 'my-theme';
$theme_root_uri = get_theme_root_uri( $theme_name );
echo $theme_root_uri;

This code snippet retrieves the URI of the theme with the name my-theme‘s root directory using the get_theme_root_uri function and then echoes the result.

How to check if the theme root URI is secure

You can also use the get_theme_root_uri function to check if the theme root URI is secure.

$theme_root_uri = get_theme_root_uri();
if ( strpos( $theme_root_uri, 'https://' ) !== false ) {
 echo 'The theme root URI is secure';
} else {
 echo 'The theme root URI is not secure';
}

This code snippet first retrieves the URI of the current theme’s root directory using the get_theme_root_uri function and then checks if it is secure by using an if statement and the strpos function to search for ‘https://’ in the URI.

Conclusion

In conclusion, the get_theme_root_uri function is a valuable tool for developers working with WordPress themes. It provides a simple and efficient way to retrieve the URI of the current theme’s root directory, allowing for easy access to theme assets and resources. By leveraging this function, developers can streamline their code and improve the performance of their themes. With its straightforward usage and powerful capabilities, get_theme_root_uri is a must-have for any WordPress theme developer.

Related WordPress Functions