on February 9, 2011 —
Having 2 Opinions!
Choose the Correct Theme Path and URL
Any plugin that deals with visual interactions will most of the time come with a default style sheet. Some plugins will even allow their users to conveniently use a custom style sheet to replace that default one.
Typically, a user does that by placing the same default style sheet, but with modified contents, in his/her theme’s folder. The snippet below is often used to make that possible:
- if (@file_exists(TEMPLATEPATH . "/default-stylesheet.css"))
- $css_url = get_bloginfo("template_url") . "/default-stylesheet.css";
- // Enqueue the style sheet
- wp_enqueue_style('etc');
if (@file_exists(TEMPLATEPATH . "/default-stylesheet.css"))
$css_url = get_bloginfo("template_url") . "/default-stylesheet.css";
// Enqueue the style sheet
wp_enqueue_style('etc');This should work well in most cases, except for one: when the user uses a child theme1 instead of a normal theme.
As you might have guessed, anything related to template (TEMPLATEPATH and template_url) is referring to the parent theme. In the above case, TEMPLATEPATH will return the path to the parent theme’s folder rather than the child theme’s.
If the user, therefore, puts the modified style sheet into a child theme’s folder, a plugin will surely fail to check if a custom style sheet exists, thus telling itself to use the default one all the time. Users will then have to modify the default CSS file or copy the file to the parent theme’s folder, neither of which is a recommended approach.
Plugin developers can fix this easily by using the correct theme path that is compatible with both parent themes and child themes, like so:
- if (@file_exists(STYLESHEETPATH . "/default-stylesheet.css"))
- $css_url = get_bloginfo("stylesheet_directory") . "/default-stylesheet.css";
- // Enqueue the style sheet
- wp_enqueue_style('etc');
if (@file_exists(STYLESHEETPATH . "/default-stylesheet.css"))
$css_url = get_bloginfo("stylesheet_directory") . "/default-stylesheet.css";
// Enqueue the style sheet
wp_enqueue_style('etc');That’s right, changing TEMPLATEPATH to STYLESHEETPATH and template_url to stylesheet_directory is all it takes to make child theme users happy. If you still use the incorrect path in your plugins, make the changes… now!








Great tutorial, but how could we get the child theme folder directory?
I tried:
function get_childTheme_url() { return dirname( get_bloginfo('stylesheet_url') ); }by using
$template_path = get_childTheme_url();to get the child theme directory without any success.You can simply use
$template_path = STYLESHEETPATH;, no need for a function actually.