Use echo home_url() instead of bloginfo('url') According to Wordpress Reference?
In the video, Zac talked about using
<?php bloginfo('url'); ?>
to get the URL of the site. However, on
bloginfo()'s WordPress reference page, here is what it says about using
'url' as parameter:
Displays the “Site address (URL)” set in Settings > General. This data is retrieved from the “home” record in the
wp_optionstable. Consider echoing
So instead of doing this according to Zac:
<h1><a class='current' href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a></h1>
should we use this instead?
<h1><a class='current' href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a></h1>
I tried the latter, seems to work for me.
So the real question is, why does WordPress recommend
echo home_url() over
Chris Shaw26,611 Points
The biggest advantage to
home_url is it allows for a custom url scheme, what does that mean? When you enter you website url into the WordPress options page, you do so by pre-pending
http:// to the start of it which is the scheme, the
home_url helper function allows you to override this when you need a secure link (
https://) instead of a non-secure link.
bloginfo function doesn't do this, instead it simply returns the value that came from the input field which would result in a non-secure link being used. You can read more about
home_url on the reference page using the below link.