get_template_directory() vs bloginfo( ‘template_directory’ ) vs TEMPLATEPATH

I was reading this article: Common WordPress Development Mistakes and How to Fix Them, and in it, they author says:

Getting the theme location: If you are using TEMPLATEPATH or bloginfo(
‘template_directory’). Stop! You should be using the very useful
get_template_directory() as seen in my examples above.

But he doesn’t elaborate. What’s so bad about bloginfo()?

1

To make a long story short: get_bloginfo( 'template_directory' ) and get_bloginfo( 'template_url' ) simply return get_template_directory_uri().

So, you can shortcut that second call simply by referring directly to the latter template tag.

Refer to source for get_bloginfo().

A few others:

  • 'url' => home_url()
  • 'wpurl' => site_url()
  • 'stylesheet_url' => get_stylesheet_uri()
  • 'stylesheet_directory' => get_stylesheet_directory_uri()
  • 'locale' => get_locale()

Edit

Note also: TEMPLATEPATH and STYLESHEETPATH are slated for deprecation, so you are better off simply replacing them now, with get_template_directory() and get_stylesheet_directory(), respectively.

Edit 2

Re: this comment

Just what I was about to say :). I’d add that get_template_directory() returns the path and get_template_directory_uri() the url. So the author isn’t being consistent: get_bloginfo( 'template_directory' ) and get_template_directory() return different things!

Refer back to source 🙂

case 'template_directory':
case 'template_url':
    $output = get_template_directory_uri();
    break;

Re: this comment:

The one caveat when developing child themes you’ll need to use get_stylesheet_directory() explicitly. Someone correct me if I’m wrong.

True, but that’s always been the case. get_bloginfo() doesn’t return anything related to get_stylesheet_directory(). It only returns get_stylesheet_uri() and get_stylesheet_directory_uri().

Leave a Comment