wppaste
WordPress

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.

Retrieves template directory path for the active theme.

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()?

The function builds its return value from get_theme_root(), which resolves to a server path, not a web-accessible URL. It was never meant for output in markup.

Why does get_template_directory() point at my parent theme instead of the child theme I'm working in?

The function calls get_template(), which always returns the parent theme's directory name, even when a child theme is the active theme.

Why does concatenating a filename onto get_template_directory() sometimes produce a malformed path?

The function returns "$theme_root/$template" with no trailing slash, so appending a filename without your own separator collapses the two segments together.

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

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
17

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

Plugin surface
1 hook

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

Called by
35

35 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()

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.

WhenInstructionsCalls it makes
always17get_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:

  1. apply_filters( template_directory )filterline 351 (+14 into the body)

    Filters the active theme directory path.

Uses · 3

Used by · 35

Show all 35

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.

  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.

6.4.1
Memoization removed.from the docblock
6.4.0
Memoizes filter execution so that it only runs once for the current theme.from the docblock
1.5.0
Introduced.from the docblock

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.