wppaste
WordPress

wp_get_theme( string $stylesheet = '', string $theme_root = '' ): WP_Theme

Since
3.4.0
Source
wp-includes/theme.php:117

Instantiates a WP_Theme object for the given theme stylesheet, or for the active theme when no argument is passed. The object is created even when the theme folder does not exist, so code must call the returned object's exists() method before trusting its data. Use $theme_root only when the theme lives somewhere get_raw_theme_root() would not find on its own, such as a directory registered with register_theme_directory().

Gets a WP_Theme object for a theme.

Compatibility

WordPress
since 3.4.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.

Parameters

$stylesheetstringoptional
Directory name for the theme. Defaults to active theme.Default: ''
$theme_rootstringoptional
Absolute path of the theme root to look in.
If not specified, get_raw_theme_root() is used to calculate the theme root for the $stylesheet provided (or active theme).Default: ''

Return value

WP_Theme
Theme object. Be sure to check the object's exists() method if you need to confirm the theme's existence.

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 name and version of the currently active theme

Calling wp_get_theme() with no arguments returns a WP_Theme object for whatever theme is active on the site.

$theme = wp_get_theme();

printf(
	'Active theme: %s (version %s)',
	esc_html( $theme->get( 'Name' ) ),
	esc_html( $theme->get( 'Version' ) )
);

Check whether a theme stylesheet exists before doing anything with it

Because wp_get_theme() always returns a WP_Theme object, guard code that switches or configures a theme with an exists() check first.

$stylesheet = 'not-a-real-theme-slug';
$theme      = wp_get_theme( $stylesheet );

if ( $theme->exists() ) {
	echo esc_html( $theme->get( 'Name' ) ) . ' is installed.';
} else {
	echo 'No theme found with the stylesheet "' . esc_html( $stylesheet ) . '".';
}

exists() is the only reliable way to tell a real theme from a made-up stylesheet, since the constructor never returns false.

Common problems and fixes · 3

Why does wp_get_theme() with a fake stylesheet name not return false?

The function always ends with return new WP_Theme( $stylesheet, $theme_root ), so it hands back an object no matter what you pass in. There is no built-in falsey signal for a missing theme. - Call $theme->exists() and branch on that. - Do not use wp_get_theme( $slug ) in an if() check by itself; it is always truthy.

Why did my custom theme directory get ignored when I called wp_get_theme()?

When $theme_root is left empty, the function calls get_raw_theme_root( $stylesheet ) and, if that path is not already registered in the global $wp_theme_directories array, prefixes it with WP_CONTENT_DIR instead of using it as an absolute path. If your theme lives outside wp-content/themes and was not added with register_theme_directory(), the calculated root will be wrong. - Register the directory with register_theme_directory() before calling wp_get_theme(). - Or pass the correct absolute path explicitly as $theme_root.

Why does calling wp_get_theme() with no arguments give me the parent theme instead of the child theme I expected, or vice versa?

With $stylesheet empty, the function falls back to get_stylesheet(), which is always the active (child) theme's directory name, not the template (parent) theme. - Use wp_get_theme() or get_stylesheet() for the active/child theme. - Use get_template() together with wp_get_theme( get_template() ) if you specifically need the parent theme.

Alternatives and related functions

wp_get_themes
When you need every installed theme rather than one specific stylesheet, since it returns an array of WP_Theme objects keyed by stylesheet.
get_stylesheet
When you only need the active theme's directory name as a plain string, not a full WP_Theme object.
get_template
When you specifically need the parent theme's stylesheet in a child theme setup, rather than the active child theme.
switch_theme
When the goal is to activate a theme rather than just read its data.

Performance profile

How much work a call to wp_get_theme() 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
12–28

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 31.

Plugin surface
None

Nothing here hands control to plugin code.

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()one call below wp_get_theme()
  • optionoption read or writeget_option()one call below wp_get_theme()

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 · 4 distinct outcomes

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

WhenInstructionsCalls it makes
!empty($stylesheet) && !empty($theme_root)12none
empty($stylesheet) && !empty($theme_root)15get_stylesheet()
!empty($stylesheet) && empty($theme_root)21–25get_raw_theme_root()
empty($stylesheet) && empty($theme_root)24–28get_stylesheet(), get_raw_theme_root()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3112–284
8.53112–284
8.43112–2843 fewer instructions than PHP 8.3
8.33412–314
8.23412–314
8.13412–314
7.43412–314

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.

Uses · 3

Used by · 50

Show all 50

Source code

function wp_get_theme( $stylesheet = '', $theme_root = '' ) {	global $wp_theme_directories; 	if ( empty( $stylesheet ) ) {		$stylesheet = get_stylesheet();	} 	if ( empty( $theme_root ) ) {		$theme_root = get_raw_theme_root( $stylesheet );		if ( false === $theme_root ) {			$theme_root = WP_CONTENT_DIR . '/themes';		} elseif ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) {			$theme_root = WP_CONTENT_DIR . $theme_root;		}	} 	return new WP_Theme( $stylesheet, $theme_root );}

Changelog

Introduced in 3.4.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.