Get WordPress Theme Information
When you create a WordPress theme you need to add comments to the top of the stylesheet so that WordPress will understand that this is the main stylesheet for your theme.
The information that will be placed in the comments of the stylesheet are:
- Theme Name
- Theme URI
- Author URI
/* Theme Name: Twenty Ten Theme URI: http://wordpress.org/ Description: The 2010 default theme for WordPress. Author: wordpressdotorg Author URI: http://wordpress.org/ Version: 1.0 Tags: black, blue, white, two-columns, fixed-width, custom-header, custom-background, threaded-comments, sticky-post, translation-ready, microformats, rtl-language-support, editor-style, custom-menu (optional) License: License URI: General comments (optional). */
This will provide WordPress all the details of the theme that it needs. WordPress will use this data in the theme admin screen to give the user as much information as they need to define the themes they want to choose.
But this information in stylesheet can also be information to the theme developer. For example if you are creating a theme options page and want to display the name of the theme then you can use the name you have defined in the style.
Before WordPress 3.4
Wordpress has an inbuilt function to get the information inside the theme comments in the stylesheet, this function is called get_theme_data().
<?php get_theme_data( $theme_filename ); ?>
The parameter you need pass into is the path to your themes stylesheet
This will return an array with all the data available in the stylesheet.
$theme_name = 'twentyeleven'; $theme_data = get_theme_data( get_theme_root() . '/' . $theme_name . '/style.css' ); echo $theme_data['Title']; echo $theme_data['Author'];
After WordPress 3.4
The above function get_theme_data() as off WordPress 3.4 has been deprecated and you will need to change any of your existing plugins/themes that use this function.
It has been replaced by the WordPress function wp_get_theme() which will return the new object
You can use it to get the same variable as before but it is done with an object and not an array.
$theme = wp_get_theme( );
Then you can use this object to display all the information of the current theme.
// Theme name echo $theme->name; // Theme title echo $theme->title; // Theme version echo $theme->version; // Get parent theme name echo $theme->parent_theme; // Get the path to the template directory echo $theme->template_dir; // Get the path to the stylesheet echo $theme->stylesheet_dir; // Get the template directory name echo $theme->template; // Get the stylesheet directory name echo $theme->stylesheet; // Return the URL to the screenshot echo $theme->screenshot; // Get the theme description echo $theme->description; // Get the theme author name echo $theme->author; // Get the theme tags echo $theme->tags;
Learn how to code with Treehouse
- Learn projects with access to 1000+ videos
- Practice live with our Code Challenge Engine
- Get help in our members-only forums
- Access 100s of premium tutorials and downloadable content
- Members content consists of premium WordPress plugins
- CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
- In-depth development tutorials
- Priority tutorial requests
- No ads