Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development WordPress Header and Footer Templates Porting existing headers and footers into WordPress

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_options table. Consider echoing home_url() instead.

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 bloginfo('url')?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Charlie,

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.

The 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.

https://developer.wordpress.org/reference/functions/home_url/

Happy coding!