wp_get_theme()WP 3.4.0

Gets the WP_Theme object, which contains information about the current theme.

An analogous function for obtaining plugin data: get_plugins().

1 time — 0.00056 sec (slow) | 50000 times — 2.02 sec (fast) | PHP 7.0.5, WP 4.5.2

No Hooks.

Returns

WP_Theme. An instance of the WP_Theme object.

Usage

$theme = wp_get_theme( $stylesheet, $theme_root );
$stylesheet(string)
The name of the directory where the theme is located, usually it matches the name of the theme itself. By default, the name of the current theme.
Default: null
$theme_root(string)
The full path to the directory where the theme is located. By default, the path is obtained by the get_raw_theme_root() function.
Default: null

Additional Information

Data can be accessed as object properties:

$theme = wp_get_theme();

print_r( [
	'name'           => $theme->name,
	'title'          => $theme->title,
	'version'        => $theme->version,
	'parent_theme'   => $theme->parent_theme,
	'template_dir'   => $theme->template_dir,
	'stylesheet_dir' => $theme->stylesheet_dir,
	'template'       => $theme->template,
	'stylesheet'     => $theme->stylesheet,
	'screenshot'     => $theme->screenshot,
	'description'    => $theme->description,
	'author'         => $theme->author,
	'tags'           => $theme->tags,
	'theme_root'     => $theme->theme_root,
	'theme_root_uri' => $theme->theme_root_uri,
] );

/* We will get:
Array (
	[name]           => Twenty Twenty-Three
	[title]          => Twenty Twenty-Three
	[version]        => 1.6
	[parent_theme]   =>
	[template_dir]   => /home/wordpress/wp-content/themes/twentytwentythree
	[stylesheet_dir] => /home/wordpress/wp-content/themes/twentytwentythree
	[template]       => twentytwentythree
	[stylesheet]     => twentytwentythree
	[screenshot]     => screenshot.png
	[description]    => Twenty Twenty-Three is designed ...
	[author]         => <a href="https://wordpress.org">the WordPress team</a>
	[tags]           => Array (
		[0] => one-column
		[1] => custom-colors
		[2] => custom-menu
	)
	[theme_root]     => /home/wordpress/wp-content/themes
	[theme_root_uri] => https://exmaple.com.com/wp-content/themes
)
*/

Also, data can be accessed as array elements:

$theme = wp_get_theme();

print_r( [
	'Name'             => $theme['Name'],
	'Version'          => $theme['Version'],
	'Status'           => $theme['Status'],
	'Title'            => $theme['Title'],
	'Author'           => $theme['Author'],
	'Author Name'      => $theme['Author Name'],
	'Author URI'       => $theme['Author URI'],
	'Description'      => $theme['Description'],
	'Template'         => $theme['Template'],
	'Stylesheet'       => $theme['Stylesheet'],
	'Template Files'   => $theme['Template Files'],
	'Stylesheet Files' => $theme['Stylesheet Files'],
	'Template Dir'     => $theme['Template Dir'],
	'Stylesheet Dir'   => $theme['Stylesheet Dir'],
] );

/*
array(
	[Name]         => Twenty Twenty-Three
	[Version]      => 1.6
	[Status]       => publish
	[Title]        => Twenty Twenty-Three
	[Author]       => <a href="https://wordpress.org">the WordPress team</a>
	[Author Name]  => the WordPress team
	[Author URI]   => https://wordpress.org
	[Description]  => Twenty Twenty-Three is designed to take advantage ....
	[Template]     => twentytwentythree
	[Stylesheet]   => twentytwentythree
	[Template Files] => Array(
		[patterns/call-to-action.php] => /home/wordpress/wp-content/themes/twentytwentythree/patterns/call-to-action.php
		[patterns/footer-default.php] => /home/wordpress/wp-content/themes/twentytwentythree/patterns/footer-default.php
	)
	[Stylesheet Files] => Array(
		[style.css] => /home/wordpress/wp-content/themes/twentytwentythree/style.css
	)
	[Template Dir]  => /home/wordpress/wp-content/themes/twentytwentythree
	[Stylesheet Dir] => /home/wordpress/wp-content/themes/twentytwentythree
)
*/

Examples

0

#1 See what the resulting object looks like

If we use this function on the default 'twentyfourteen' theme, we get:

print_r( wp_get_theme() );

Will get:

WP_Theme Object
(
	[theme_root:WP_Theme:private] => C:\sites\example.com\www/wp-content/themes
	[headers:WP_Theme:private] => Array
		(
			[Name] => Twenty Fourteen
			[ThemeURI] => http://wordpress.org/themes/twentyfourteen
			[Description] => In 2014, our default theme lets you ....
			[Author] => the WordPress team
			[AuthorURI] => http://wordpress.org/
			[Version] => 1.0
			[Template] => 
			[Status] => 
			[Tags] => black, green, white, light, dark, ...
			[TextDomain] => twentyfourteen
			[DomainPath] => 
		)

	[headers_sanitized:WP_Theme:private] => 
	[name_translated:WP_Theme:private] => 
	[errors:WP_Theme:private] => 
	[stylesheet:WP_Theme:private] => twentyfourteen
	[template:WP_Theme:private] => twentyfourteen
	[parent:WP_Theme:private] => 
	[theme_root_uri:WP_Theme:private] => 
	[textdomain_loaded:WP_Theme:private] => 
	[cache_hash:WP_Theme:private] => ea3ba1457a0fbfd275006de061bbffe5
)
0

#2 Display the name of the currently active theme

echo wp_get_theme(); // Twenty Twenty-Three
0

#3 Display the name of the installed theme

$my_theme = wp_get_theme( 'twentyten' );

if ( $my_theme->exists() ) {
	echo esc_html( $my_theme );
}
0

#4 Display the version of the current theme

$my_theme = wp_get_theme();

echo esc_html( $my_theme->get( 'Name' ) . " version " . $my_theme->get( 'Version' ) );
0

#5 Display the URL of the author of the current theme

$my_theme = wp_get_theme();
echo esc_html( $my_theme->get( 'AuthorURI' ) );
0

#6 Display other data of the current theme

$my_theme = wp_get_theme();

echo esc_html( $my_theme->get( 'TextDomain' ) );
echo esc_html( $my_theme->get( 'ThemeURI' ) );

Notes

  • Global. String[]. $wp_theme_directories

Changelog

Since 3.4.0 Introduced.

wp_get_theme() code WP 7.0

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