wp_get_theme()WP 3.4.0

Gets a WP_Theme object for a theme.

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

No Hooks.

Return

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

Usage

wp_get_theme( $stylesheet, $theme_root );
$stylesheet(string)
Directory name for the theme.
Default: active theme
$theme_root(string)
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: ''

Examples

0

#1 See what the resulting object looks like

If we use this function on the default 'twentyfourteen' theme, we 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 esc_html( wp_get_theme() ); // Twenty Fourteen
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. Array. $wp_theme_directories

Changelog

Since 3.4.0 Introduced.

Code of wp_get_theme() WP 6.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 );
}