Theme Information – How To Get It Right In WordPress 3.4

WordPress 3.4 introduces many gorgeous things like theme customizer, twitter embed, HTML caption, etc. Among those highlights, there’re many improvements and changes in the code we should know, too. In this post, I’ll talk about a proper way to get theme information via wp_get_theme function. This function replaces the old function get_theme_data which was used in previous WordPress versions and now is deprecated.

Get Theme Information In WordPress Theme Information   How To Get It Right In WordPress 3.4

Start getting theme information by calling $theme = wp_get_theme() which returns a WP_Theme object for current theme. The object contains a magic function __get that helps you get the theme information easily like reference to its properties (even when those properties don’t exist). Here are the list of them, all are self-explanatory (but I still add some explanation to make them clearer):

  • $theme->name or $theme->title
  • $theme->version
  • $theme->parent_theme: parent theme name
  • $theme->template_dir: full path to template directory, e.g. parent theme’s directory
  • $theme->stylesheet_dir: full path to stylesheet directory, e.g. child theme’s directory
  • $theme->template: template directory name
  • $theme->stylesheet: stylesheet directory name
  • $theme->screenshot: URL of screenshot
  • $theme->description
  • $theme->author: with HTML markup, e.g. a link to author’s homepage if it presents
  • $theme->{'Author Name'}: author name in plain text
  • $theme->{'Author URI'}: link to author’s homepage
  • $theme->tags
  • $theme->theme_root: path to themes folder
  • $theme->theme_root_uri: URL to themes folder

If you need simply displaying theme name, you can echo the $theme directly.

If you need to get information about another theme, just pass the theme slug into the wp_get_theme() function, like this:

$twentyeleven = wp_get_theme( 'twentyeleven' );
echo $twentyeleven->version;

I see some premium WordPress themes and frameworks show their theme information in a meta box in settings page. So, if you’re doing the same thing, you might want to update your theme now. That also improve your code quality, too!

Leave a Reply