WordPress – bloginfo vs get_xxx_directory_uri

When I first started using WordPress, I found out about the bloginfo function, which gives you a lot of information about your blog. Normally I would use it to denote things like the site name (bloginfo(‘name’)), the blog description (bloginfo(‘description’)), and even to link to my scripts (bloginfo(‘template_directory’)).

I found out late about the functions get_template_directory_uri() and get_stylesheet_directory_uri().  What I couldn’t wrap my head around was which one was better to use. These new functions did the same as bloginfo(‘template_directory’) and bloginfo(‘stylesheet_directory’), so why should I use the other longer one?  I decided that I didn’t want to use these new codes because the usage I had decided to go with was working.

However, the design industry only grows as new technology is adopted. So I decided I would try to figure out which of these is better.  In my study, I’ve found that using get_template_directory_uri() and get_stylesheet_directory_uri() is better than using bloginfo for one good reason.  bloginfo(‘template_directory’) will not check for SSL. So even if you are on a secure page (like a checkout page), bloginfo will return http:// version.  This means that in some browsers, your stylesheets will not load, and you’ll have a broken site.

get_xxx_directory_uri() checks for SSL and will return the correct beginning to your URL (either http:// or https://).  So now that we know which function set works better, how do we decide which one to use:

get_template_directory_uri() and get_stylesheet_directory_uri() will return the same thing if you are not using a child theme. If you are using a child theme, get_template_directory_uri() will return the page to the parent theme folder, while get_stylesheet_diretory_uri() will retun the child theme folder.

One Comment on “WordPress – bloginfo vs get_xxx_directory_uri”

  • Adding a Custom Javascript or jQuery Script Files to WordPress Theme or Child Theme | bigTreeWeb February 19th, 2014 5:22 am

    […] get_stylsheet_directory_uri () says this is a file in the Child Theme directory, or where the file is located in relationship to the parent or child theme. If this were a parent theme you would use get_template_directory_uri(). Some people have suggested using get_bloginfo (‘stylesheet_directory’) instead. However I read an article to see what the difference was and decided not to do it that way, even though it worked (see page at this link for reference: http://www.klongdesigns.com/wordpress-bloginfo-vs-get_xxx_directory_uri/). […]

Leave a Reply