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()?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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().


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x