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.
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?
- Can I use get_template_directory_uri() to require a PHP file from the theme?
- I filtered template_directory_uri and now other parts of the theme look broken, why?
Why does get_template_directory_uri() give me the parent theme's URL instead of my child theme's?
Can I use get_template_directory_uri() to require a PHP file from the theme?
I filtered template_directory_uri and now other parts of the theme look broken, why?
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
- Scaling
- Constant
- Instructions
- 21
- Plugin surface
- 1 hook
- Called by
- 50
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 21.
Third-party callbacks on 'template_directory_uri' run inside this call, and their cost is not bounded by anything here.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 21 | get_template(), rawurlencode(), get_theme_root_uri(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 21 | 21 | 0 | |
| 8.5 | 21 | 21 | 0 | |
| 8.4 | 21 | 21 | 0 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 25 | 25 | 0 | |
| 8.2 | 25 | 25 | 0 | |
| 8.1 | 25 | 25 | 0 | |
| 7.4 | 25 | 25 | 0 |
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:
- apply_filters( template_directory_uri )filterline 375 (+14 into the body)
Filters the active theme directory URI.
Uses · 3
- get_template()Retrieves name of the active theme.
- get_theme_root_uri()Retrieves URI for themes directory.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 50
- Custom_Image_Header::get_default_header_images()Gets the details of default header images if defined.
- Custom_Image_Header::process_default_headers()Processes the default headers.
- Custom_Image_Header::reset_header_image()Resets a header image to the default image for the theme.
- Custom_Image_Header::step_1()Displays first step of custom header image page.
- Featured_Content::enqueue_scripts()Enqueues the tag suggestion script.
- Twenty_Twenty_One_Dark_Mode::customize_controls_enqueue_scripts()Enqueues scripts for the customizer.
- Twenty_Twenty_One_Dark_Mode::editor_custom_color_variables()Enqueues editor custom color variables & scripts.
- Twenty_Twenty_One_Dark_Mode::enqueue_scripts()Enqueues scripts and styles.
- Twenty_Twenty_One_Dark_Mode::the_script()Prints the dark-mode switch script.
- WP_Customize_Manager::register_controls()Registers some default controls.
- WP_REST_Themes_Controller::prepare_item_for_response()Prepares a single theme output for response.
- WP_URL_Pattern_Prefixer::get_default_contexts()Returns the default contexts used by the class.
Show all 50
- _get_random_header_data()Gets random header image data from registered images in theme.
- get_bloginfo()Retrieves information about the current site.
- get_custom_header()Gets the header image data.
- get_editor_stylesheets()Retrieves any registered editor stylesheet URLs.
- get_parent_theme_file_uri()Retrieves the URL of a file in the parent theme.
- get_theme_file_uri()Retrieves the URL of a file in the theme.
- get_theme_mod()Retrieves theme modification value for the active theme.
- twenty_twenty_one_get_starter_content()Returns the array of starter content for the theme.
- twenty_twenty_one_register_block_pattern()Registers Block Patterns.
- twenty_twenty_one_scripts()Enqueues scripts and styles.
- twentyeleven_admin_enqueue_scripts()Enqueues styles and scripts for the theme options page.
- twentyeleven_block_editor_styles()Enqueues styles for the block-based editor.
- twentyeleven_color_schemes()Returns an array of color schemes registered for Twenty Eleven.
- twentyeleven_customize_preview_js()Binds JS handlers to make Customizer preview reload changes asynchronously.
- twentyeleven_enqueue_color_scheme()Enqueues the styles for the current color scheme.
- twentyeleven_layouts()Returns an array of layout options registered for Twenty Eleven.
- twentyeleven_scripts_styles()Enqueues scripts and styles for front end.
- twentyfifteen_block_editor_styles()Enqueues styles for the block-based editor.
- twentyfifteen_customize_control_js()Binds JS listener to make Customizer color_scheme control.
- twentyfifteen_customize_preview_js()Binds JS handlers to make the Customizer preview reload changes asynchronously.
- twentyfifteen_fonts_url()Registers fonts for Twenty Fifteen.
- twentyfifteen_scripts()Enqueues scripts and styles.
- twentyfifteen_setup()Sets up theme defaults and registers support for various WordPress features.
- twentyfourteen_admin_fonts()Enqueues font stylesheet to admin screen for custom header display.
- twentyfourteen_block_editor_styles()Enqueues styles for the block-based editor.
- twentyfourteen_customize_preview_js()Binds JS handlers to make Customizer preview reload changes asynchronously.
- twentyfourteen_font_url()Registers Lato font for Twenty Fourteen.
- twentyfourteen_scripts()Enqueues scripts and styles for the front end.
- twentyfourteen_setup()Twenty Fourteen setup.
- twentynineteen_scripts()Enqueues scripts and styles.
- twentyseventeen_block_editor_styles()Enqueues styles for the block-based editor.
- twentyseventeen_fonts_url()Registers custom fonts.
- twentyseventeen_scripts()Enqueues scripts and styles.
- twentyseventeen_setup()Sets up theme defaults and registers support for various WordPress features.
- twentysixteen_block_editor_styles()Enqueues styles for the block-based editor.
- twentysixteen_customize_control_js()Binds the JS listener to make Customizer color_scheme control.
- twentysixteen_customize_preview_js()Binds JS handlers to make the Customizer preview reload changes asynchronously.
- twentysixteen_fonts_url()Registers fonts for Twenty Sixteen.
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.
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.