Using get_theme_data() when Developing a Custom Theme for WordPress
by Bandicoot Marketing on | Posted in Tutorials | 1 comment
If you are building a custom theme that you would like to add to the WordPress directory or start to sell online somewhere, a great function that can help you out is
get_theme_data(). What exactly does it do? It gathers all the theme information that you were supposed to add to the
style.css file so you can use it throughout your theme.
If you download a copy of Magazine Basic, you can see it in action in the
functions.php file, right at the top. To make sure it is working, though, you have to have your style.css file setup correctly. The top of that file should look something like this:
/* Theme Name: Magazine Basic Theme URI: http://themes.bavotasan.com/our-themes/basic-themes/magazine-basic Description: Built for WordPress 3.0. A magazine style theme with a fully customizable layout. Theme options include site width, 1 or 2 widgetized sidebars, header logo, multiple front page grid layouts, new WP 3.0 menu system, Google Analytics, pagination, drop-down menus and tons more. Also includes dynamic SEO keywords and site descriptions. Tested in Firefox 3.6, IE 7 & 8 and Safari 4. Fully optimized for search engine ranking. Translation ready. 100% valid xHTML. Designed by <a href="http://themes.bavotasan.com/">Themes by bavotasan.com</a>. Version: 2.6.7 Author: c.bavota Author URI: http://themes.bavotasan.com/ Tags: right-sidebar,left-sidebar,fixed-width,three-columns,two-columns,white,custom-header,theme-options The CSS, XHTML and design is released under GPL: http://www.opensource.org/licenses/gpl-license.php */
This is all information that you can now use with
get_theme_data(). I use it in Magazine Basic like so:
$bavotasan_theme_data = get_theme_data(TEMPLATEPATH.'/style.css');
With that in my
functions.php file, I can use the array of data that it provides like so:
$bavotasan_theme_data = get_theme_data(TEMPLATEPATH.'/style.css'); define('THEME_NAME', $bavotasan_theme_data['Name']); define('THEME_AUTHOR', $bavotasan_theme_data['Author']); define('THEME_HOMEPAGE', $bavotasan_theme_data['URI']); define('THEME_VERSION', trim($bavotasan_theme_data['Version']));
If you are not familiar with the define() function, check out Defining a Constant with PHP.
Now you can use those four constants throughout your theme. Here are some examples that I use:
<span class="red"><?php echo THEME_NAME; ?></span> <?php _e("theme designed by", "magazine-basic"); ?> <a href="http://themes.bavotasan.com"><span class="red">Themes by bavotasan.com</span></a>.<br />
echo '<p><ul><li><strong>'.__('Version', "magazine-basic").':</strong> '.THEME_VERSION.'</li><li><strong>'.__('Author', "magazine-basic").':</strong> <a href="http://bavotasan.com/">'.THEME_AUTHOR.'</a></li><li><strong>'.__('Built by', "magazine-basic").':</strong> <a href="http://themes.bavotasan.com/">Themes by bavotasan.com</a></li><li><strong>'.__('Theme home page', "magazine-basic").':</strong> <a href="'.THEME_HOMEPAGE.'">'.THEME_NAME.'</a></li></ul></p>';
You can also get the description and the tags. Check out the Codex for a little more info.
1 comment for “Using get_theme_data() when Developing a Custom Theme for WordPress”