Back to Top

Choose the Correct Theme Path and URL

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

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

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!


  1. []

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

3 Opinions for Choose the Correct Theme Path and URL (1 Trackback)

  1. User's Gravatar
    toure July 18, 2011 at 9:56 am – Permalink

    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.

    • User's Gravatar
      OddOneOut July 18, 2011 at 12:17 pm – Permalink

      You can simply use $template_path = STYLESHEETPATH;, no need for a function actually.

  1. Ten Useful WordPress Constants You Might Love - Better WordPress

    […] You can also use this constant to check for file existence (or run tasks based on that), but this one is actually preferred over TEMPLATEPATH in most situations, and the reason is described quite well in a tip I wrote earlier about how to choose the correct theme path for child themes. […]

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.

  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: