Using WordPress ‘get_theme_root_uri()’ PHP function

The get_theme_root_uri() WordPress PHP function retrieves the URI for the themes directory without a trailing slash.

Usage

get_theme_root_uri($stylesheet_or_template, $theme_root)

Custom Example:

Input:
echo get_theme_root_uri('mytheme', '/wp-content/themes');

Output:
http://example.com/wp-content/themes

Parameters

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

More information

See WordPress Developer Resources: get_theme_root_uri()

Examples

Display Theme Root URI

Display the theme root URI on your website.

echo get_theme_root_uri();

Display Theme Root URI with Custom Theme

Display the theme root URI for a specific theme named “mytheme”.

echo get_theme_root_uri('mytheme');

Display Theme Root URI with Custom Theme Root

Display the theme root URI for a specific theme and custom theme root.

echo get_theme_root_uri('mytheme', '/wp-content/themes');

Get Theme Root URI for Enqueueing Stylesheet

Get the theme root URI for enqueueing a stylesheet in the active theme.

function enqueue_mytheme_styles() {
    $theme_root_uri = get_theme_root_uri('mytheme');
    wp_enqueue_style('mytheme-style', $theme_root_uri . '/mytheme/style.css');
}
add_action('wp_enqueue_scripts', 'enqueue_mytheme_styles');

Get Theme Root URI for Enqueueing Script

Get the theme root URI for enqueueing a script in the active theme.

function enqueue_mytheme_scripts() {
    $theme_root_uri = get_theme_root_uri('mytheme');
    wp_enqueue_script('mytheme-script', $theme_root_uri . '/mytheme/script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'enqueue_mytheme_scripts');