wppaste
WordPress

get_template_directory_uri(): string

Since
1.5.0
Source
wp-includes/theme.php:361

Builds the URL to the active theme's parent template folder by combining the theme root URI with the directory name returned by get_template(). The value passes through the 'template_directory_uri' filter before being returned, so themes and plugins can rewrite it. In a child theme setup this always resolves to the parent theme's folder, not the child's, so use get_stylesheet_directory_uri() when you need the currently active theme's own assets.

Retrieves template directory URI for the active theme.

Compatibility

WordPress
since 1.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

string
URI to active theme's template directory.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Print the active theme's template directory URI

A quick check of what URL WordPress builds for the current theme's folder.

echo esc_html( get_template_directory_uri() );

On a site with no child theme active, this returns the same value as get_stylesheet_directory_uri().

Reference a parent theme asset from a child theme's functions.php

A child theme's functions.php needs to output a background image that physically lives in the parent theme, not the child.

add_action( 'wp_head', 'wppaste_print_parent_theme_bg' );

function wppaste_print_parent_theme_bg() {
	$parent_theme_uri = get_template_directory_uri();

	printf(
		'<style id="wppaste-parent-bg">body{background-image:url(%s);}</style>' . "\n",
		esc_url( $parent_theme_uri . '/assets/images/background.jpg' )
	);

	echo '<!-- parent theme directory uri: ' . esc_html( $parent_theme_uri ) . ' -->' . "\n";
}

Load the front page after activating this snippet to see the style tag and comment in the page source.

Common problems and fixes · 3

Why does get_template_directory_uri() give me the parent theme's URL instead of my child theme's?

It calls get_template(), which always returns the parent theme's directory name, never the child's. That is by design, the function is meant to point at the template (parent) files.

Can I use get_template_directory_uri() to require a PHP file from the theme?

No, it returns a URL built from the theme root URI, not a filesystem path, so require or include on that string will fail or silently do nothing over HTTP wrappers.

I filtered template_directory_uri and now other parts of the theme look broken, why?

The 'template_directory_uri' filter runs inside this function every time it's called, and it's used by core features like Custom_Image_Header and WP_Customize_Manager, so a blanket change affects all of them.

Alternatives and related functions

get_stylesheet_directory_uri
When you need the URL of the currently active theme's own folder, which differs from this function whenever a child theme is active.
get_template_directory
When you need a filesystem path (for require, include, or file_exists) rather than a URL.
get_stylesheet_directory
When you need the filesystem path of the active (possibly child) theme instead of the parent.
get_theme_root_uri
When you only need the URL of the themes root folder itself, without the theme's own directory name appended.

Performance profile

How much work a call to get_template_directory_uri() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
21

Executed per call on PHP 8.5. The body compiles to 21.

Plugin surface
1 hook

Third-party callbacks on 'template_directory_uri' run inside this call, and their cost is not bounded by anything here.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below get_template_directory_uri()

Further down the call graph this can also reach cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_template_directory_uri() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always21get_template(), rawurlencode(), get_theme_root_uri(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev21210
8.521210
8.4212104 fewer instructions than PHP 8.3
8.325250
8.225250
8.125250
7.425250

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_template_directory_uri() runs, in this order:

  1. apply_filters( template_directory_uri )filterline 375 (+14 into the body)

    Filters the active theme directory URI.

Uses · 3

Used by · 50

Show all 50

Source code

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 );}

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.