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().
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'] ); }
#3 Using a function in an HTML tag
To be safe, the result of the function should be cleared with esc_url() or esc_attr().
This cleanup should be done for all functions which are used in tag attributes. For example, if this cleanup is not done, your code will not pass the check when placing the theme/plugin in the official repository. An example of cleaning:
<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/screenshot.png">
Changelog
Since 1.5.0 | Introduced. |
get_template_directory_uri() get template directory uri code WP 6.6.2
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 ); }