WordPress at a glance

get_template_directory_uri() WP 1.5.0

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.002132s = very slow | 50000 times = 3.37s = fast | PHP 7.1.2, WP 4.7.3
Hooks from the function
Return

String. Template directory URI.

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: wp-includes/theme.php WP 5.2.2

<?php
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 current theme directory URI.
	 *
	 * @since 1.5.0
	 *
	 * @param string $template_dir_uri The URI of the current theme directory.
	 * @param string $template         Directory name of the current theme.
	 * @param string $theme_root_uri   The themes root URI.
	 */
	return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
}

Related Functions

From tag: theme path (theme folder url)

More from category: Theme files connection

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register