Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,649 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!