get_template_directory_uri()
Retrieve current theme directory URI. Returns the URL to the root theme, not a child one. Doesn't contain a closing slash at the end.
You can also use get_bloginfo('template_url');
instead of this function.
If a child theme is used and you want to get its URL, use get_stylesheet_directory_uri().
If you want to get the path to the theme directory (folder), use get_template_directory().
If you want to get URL to the plugin, use plugin_dir_url().
1 time — 0.002132 sec (very slow) | 50000 times — 3.37 sec (fast) | PHP 7.1.2, WP 4.7.3
Hooks from the function
Return
String
. URI to active theme's template directory.
Usage
get_template_directory_uri();
Examples
#1 Get the URL to the theme
echo get_template_directory_uri(); // output: http://example.com/wp-content/themes/theme_name
#2 Including a script
add_action( 'wp_enqueue_scripts', 'my_scripts_method' ); function my_scripts_method() { wp_enqueue_script( 'custom_script', get_template_directory_uri() . '/js/custom_script.js', ['jquery'] ); }
Changelog
Since 1.5.0 | Introduced. |
Code of get_template_directory_uri() get template directory uri WP 6.0
function get_template_directory_uri() { $template = str_replace( '%2F', '/', rawurlencode( get_template() ) ); $theme_root_uri = get_theme_root_uri( $template ); $template_dir_uri = "$theme_root_uri/$template"; /** * Filters the active theme directory URI. * * @since 1.5.0 * * @param string $template_dir_uri The URI of the active theme directory. * @param string $template Directory name of the active theme. * @param string $theme_root_uri The themes root URI. */ return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri ); }