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

Gets a WP_Theme object for a theme.

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

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

Source

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

VersionDescription
3.4.0Introduced.

User Contributed Notes

  1. Skip to note 11 content

    Result:

    object(WP_Theme)[916]
      public 'update' => boolean false
      private 'theme_root' => string 'home/path/wp-content/themes' (length=77)
      private 'headers' => 
        array (size=11)
          'Name' => string 'mytheme' (length=7)
          'ThemeURI' => string 'http://example.com/' (length=22)
          'Description' => string 'Description' (length=11)
          'Author' => string 'Something Here' (length=14)
          'AuthorURI' => string 'http://example.com/' (length=22)
          'Version' => string '1.0.0' (length=5)
          'Template' => string '' (length=0)
          'Status' => string '' (length=0)
          'Tags' => string 'custom-background, custom-logo, custom-menu, featured-images, threaded-comments, translation-ready' (length=98)
          'TextDomain' => string 'mytheme' (length=7)
          'DomainPath' => string '' (length=0)
      private 'headers_sanitized' => null
      private 'name_translated' => null
      private 'errors' => null
      private 'stylesheet' => string 'mytheme' (length=7)
      private 'template' => string 'mytheme' (length=7)
      private 'parent' => null
      private 'theme_root_uri' => null
      private 'textdomain_loaded' => null
      private 'cache_hash' => string 'ca9dd01f01f2a5cb4616a776eff52690' (length=32)
  2. Skip to note 13 content

    Switch a specific theme on all sites in a multisite installation

    <?php
    function wpdocs_switch_all_multisite_themes() {
        foreach ( get_sites() as $site ) {
            switch_to_blog( $site->blog_id );
    	$blog_theme = wp_get_theme();
    	if ( 'twentyeleven' === $blog_theme ) {
                switch_theme( 'twentytwenty' );
                restore_current_blog();
    	}
        }
    }
    
    wpdocs_switch_all_multisite_themes();
    ?>
  3. Skip to note 14 content

    Get the directory name of the current theme regardless of the child theme.

    function wpdocs_get_current_theme_directory(){
        $current_theme_dir  = '';
        $current_theme      = wp_get_theme();
        if( $current_theme->exists() && $current_theme->parent() ){
            $parent_theme = $current_theme->parent();
    
            if( $parent_theme->exists() ){
                $current_theme_dir = $parent_theme->get_stylesheet();
            }
        } elseif( $current_theme->exists() ) {
            $current_theme_dir = $current_theme->get_stylesheet();
        }
    
        return $current_theme_dir;
    }
    
    echo wpdocs_get_current_theme_directory();
  4. Skip to note 15 content

    Code to Display current or any installed theme version:

    Display current theme version:

    $theme_info = wp_get_theme();
    echo $theme_info->get( 'Version' );

    Display any of installed theme version using directory name:

    $theme_info = wp_get_theme( 'twentytwentyfour' );
    echo $theme_info->get( 'Version' );
  5. Skip to note 17 content

    Display the current theme’s version

    $my_theme = wp_get_theme();
    printf( "%1$s is version %2$s",
    	$my_theme->get( 'Name' ),
    	$my_theme->get( 'Version' )
    );

You must log in before being able to contribute a note or feedback.