get_template_directory(): string
- Since
- 1.5.0, 6.4.0, 6.4.1
- Source
wp-includes/theme.php:337
Returns the absolute filesystem path to the active theme's template folder, which is the parent theme's directory whenever a child theme is active. Use get_stylesheet_directory() instead when you need the child theme's own folder, and get_template_directory_uri() when a URL is what you actually need for enqueuing assets. The returned path passes through the template_directory filter before it comes back.
Compatibility
- WordPress
- since 6.4.1
- 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- Path 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.
Get the active theme's template directory and compare it to the stylesheet directory
Print both directories to see whether the current setup is running a plain theme or a child theme.
$template_dir = get_template_directory();
$stylesheet_dir = get_stylesheet_directory();
echo esc_html( "Template directory: $template_dir" ) . "\n";
echo esc_html( "Stylesheet directory: $stylesheet_dir" ) . "\n";
if ( $template_dir === $stylesheet_dir ) {
echo esc_html( 'No child theme is active, so both paths point to the same folder.' );
} else {
echo esc_html( 'A child theme is active, so get_template_directory() points to the parent theme.' );
}On a fresh install with no child theme active, both paths will match.
Redirect the template directory with the template_directory filter
Filter the returned path, for example to point asset lookups at a fallback folder during a migration.
add_filter( 'template_directory', 'wppaste_swap_template_directory', 10, 3 );
function wppaste_swap_template_directory( $template_dir, $template, $theme_root ) {
return $theme_root . '/' . $template . '-fallback';
}
$dir = get_template_directory();
echo esc_html( "Filtered template directory: $dir" );The filter receives the unfiltered directory, the theme slug, and the theme root, in that order.
Common problems and fixes · 3
- Why does get_template_directory() give me a filesystem path instead of a URL I can use in wp_enqueue_script()?
- Why does get_template_directory() point at my parent theme instead of the child theme I'm working in?
- Why does concatenating a filename onto get_template_directory() sometimes produce a malformed path?
Why does get_template_directory() give me a filesystem path instead of a URL I can use in wp_enqueue_script()?
Why does get_template_directory() point at my parent theme instead of the child theme I'm working in?
Why does concatenating a filename onto get_template_directory() sometimes produce a malformed path?
Alternatives and related functions
get_template_directory_uri- When you need a URL to reference the theme in HTML or enqueue scripts and styles, rather than a server filesystem path.
get_stylesheet_directory- When a child theme may be active and you need that child theme's own folder rather than its parent's.
get_stylesheet_directory_uri- When you need the URL of the active (possibly child) theme's folder instead of a filesystem path.
get_theme_file_path- When you want the path to one specific file and want WordPress to automatically prefer the child theme's copy if it exists.
Performance profile
How much work a call to get_template_directory() 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
- 17
- Plugin surface
- 1 hook
- Called by
- 35
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 17.
Third-party callbacks on 'template_directory' run inside this call, and their cost is not bounded by anything here.
35 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()
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() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 17 | get_template(), get_theme_root(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 17 instructions, 17 executed per call, 0 branches. The work does not change between versions.
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() runs, in this order:
- apply_filters( template_directory )filterline 351 (+14 into the body)
Filters the active theme directory path.
Uses · 3
- get_template()Retrieves name of the active theme.
- get_theme_root()Retrieves path to themes directory.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 35
- Twenty_Twenty_One_Dark_Mode::the_script()Prints the dark-mode switch script.
- WP_Customize_Manager::prepare_starter_content_attachments()Prepares starter content attachments.
- WP_Debug_Data::get_wp_parent_theme()Gets the WordPress parent theme section of the debug data.
- WP_Theme_JSON_Resolver::get_file_path_from_theme()Builds the path to the given file and checks that it is readable.
- WP_Theme_JSON_Resolver::get_style_variations()Returns the style variations defined by the theme.
- _get_block_template_file()Retrieves the template file from the theme for a given slug.
- _get_block_templates_files()Retrieves the template files from the theme.
- _load_textdomain_just_in_time()Loads plugin and theme text domains just-in-time.
- get_attachment_icon_src()Retrieve icon URL and Path.
- get_block_asset_url()Gets the URL to a block asset.
- get_editor_stylesheets()Retrieves any registered editor stylesheet URLs.
- get_parent_theme_file_path()Retrieves the path of a file in the parent theme.
Show all 35
- get_theme_file_path()Retrieves the path of a file in the theme.
- get_theme_file_uri()Retrieves the URL of a file in the theme.
- load_theme_textdomain()Loads the theme's translated strings.
- locate_block_template()Finds a block template with equal or higher specificity than a given PHP template file.
- resolve_block_template()Returns the correct 'wp_template' to render for the request template type.
- twenty_twenty_one_setup()Sets up theme defaults and registers support for various WordPress features.
- twentyeleven_setup()Sets up theme defaults and registers support for various WordPress features.
- twentyfifteen_register_block_patterns()Registers block patterns and pattern categories.
- twentyfourteen_register_block_patterns()Registers block patterns and pattern categories.
- twentyfourteen_widgets_init()Registers three Twenty Fourteen widget areas.
- twentynineteen_register_block_patterns()Registers block patterns and pattern categories.
- twentyseventeen_register_block_patterns()Registers block patterns and pattern categories.
- twentysixteen_register_block_patterns()Registers block patterns and pattern categories.
- twentyten_register_block_patterns()Registers block patterns and pattern categories.
- twentyten_setup()Sets up theme defaults and registers support for various WordPress features.
- twentythirteen_register_block_patterns()Registers block patterns and pattern categories.
- twentytwelve_register_block_patterns()Registers block patterns and pattern categories.
- twentytwenty_register_block_patterns()Registers block patterns and pattern categories.
- twentytwenty_theme_support()Sets up theme defaults and registers support for various WordPress features.
- validate_current_theme()Checks that the active theme has the required files.
- wp_set_template_globals()Set up the globals used for template loading.
- wp_templating_constants()Defines templating-related WordPress constants.
- wp_theme_has_theme_json()Checks whether a theme or its parent has a theme.json file.
Source code
function get_template_directory() { $template = get_template(); $theme_root = get_theme_root( $template ); $template_dir = "$theme_root/$template"; /** * Filters the active theme directory path. * * @since 1.5.0 * * @param string $template_dir The path of the active theme directory. * @param string $template Directory name of the active theme. * @param string $theme_root Absolute path to the themes directory. */ return apply_filters( 'template_directory', $template_dir, $template, $theme_root );}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.