With WordPress 3.0 multisite and the rise of child theming, it’s sometimes tricky to know which function or constant is appropriate to call the css file or template needed in a theme. Here’s some common examples and best practices.
(Also, see Justin Tadlock’s more complete tutorial on this subject)
WordPress Constants
There a few helpful constants defined in WordPress for theme developers. TEMPLATEPATH is defined as get_template_directory(), and STYLESHEETPATH is defined as get_stylesheet_directory().
The function call and the constant are actually interchangeable with each other, but by using the constants (TEMPLATEPATH & STYLESHEETPATH) rather than the functions each time, you’ll reduce the amount of database queries and speed up your site.
TEMPLATEPATH
This returns the path to the template files of a theme. If a child theme is activated, this path will still point to the parent theme. An example path would be “/var/www/html/mysite/wp-content/themes/parenttheme”. You would most likely use this call to include a file. Example code in the functions.php file:
// Create Theme Options Page require_once(TEMPLATEPATH . '/extensions/theme-options.php');
bloginfo(‘template_directory’);
bloginfo(‘template_directory’) returns the URL of the template directory. An example of this would be “http://mysite.com/multisite1/wp-content/themes/parenttheme”. This could be used to call a stylesheet or an image file. If a child theme is activated, this still returns the parent template directory. Example code in the header.php file:
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('template_directory'); ?>/css/ie6.css" />
<![endif]-->
STYLESHEETPATH
This returns the stylesheet path to the template files of a theme. If a child theme is activated, this path will point to the child theme directory. An example path would be “/var/www/html/mysite/wp-content/themes/childtheme”. You would most likely use this call to include a file that you are sure is present in a child theme, or to check if it is. Example code in the functions.php file:
// Create Theme Options Page for the Child Theme require_once(STYLESHEETPATH . '/extensions/theme-options.php');
get_bloginfo(‘stylesheet_directory’)
bloginfo(‘stylesheet_directory’) returns the URL of the child template directory. An example of this would be “http://mysite.com/multisite1/wp-content/themes/childtheme”. This could be used to call a stylesheet or an image file in a child theme. Example code in the header.php file:
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('stylesheet_directory'); ?>/css/ie6.css" />
<![endif]-->
Define Your Own Constants
If your theme required a lot of calls to get_bloginfo(‘stylesheet_directory’), you should define this as a new constant to reduce database calls and make your code easier to read. To do this, place the following snippet in your functions.php file (it works in child themes as well):
define('STYLESHEET_DIR', get_bloginfo('stylesheet_directory'));
To retrieve the URL, you can use this anywhere in your theme:
<?php echo STYLESHEET_DIR; ?>
Thank you SO much for this! I was going nuts trying to figure out how to modify the functions.php in a TwentyTen child theme on a Multisite network so that I could include my own custom headers for all the subsites. The child theme resides in the main site’s directories, so “%s” was pointing to nonexistent subsite theme directories. I defined a constant for my_childtheme_stylesheet_dir and could thus point to the custom headers like so: ‘url’ => my_childtheme_stylesheet_dir . ‘/images/headers/my-custom-header.png’.
Child Images not found when called in the header were driving me crazy… You solved that problem and I thank you!