3

Both functions return the website url.

And, as home_url() needs sanitization (for example <?php echo esc_url( home_url( '/' ) ); ?>), why it is this snippet of code, instead of <?php echo get_ home_url( '/' ); ?>, that we found in Wordpress Codex and Wordpress Template Twenty Nineteen ?

2
1

There isn't much difference but they are not the same.

get_home_url

get_home_url() takes null or a blog id as the first parameter. As per documentation here.

get_home_url( int $blog_id = null, string $path = '', string|null $scheme = null )

If you are dealing with multiple homes (as in, say a multi-site set up) this might be useful.

home_url

home_url(), on the other hand, is less fussed about per blog settings and just wants the home URL. As per documentation here.

home_url( string $path = '', string|null $scheme = null )

It is the equivalent of calling get_home_url( null, $path, $scheme );. Most of the time, this is the function you want.

1
  • 1
    If i understand you well, as get_home_url() is taking this blog id as a parameter, it makes more sense for a standard wordpress template as Twenty Nineteen to use it in the header.php, cause it could be used in a multisite set up, which is relevant because it's pretty common. Thanks a lot for your very clear and pedagogic answer.
    – PhpDoe
    Sep 13 '19 at 16:50
2

There is no difference. If you look at wordpress code you will see

function home_url( $path = '', $scheme = null ) {
        return get_home_url( null, $path, $scheme );
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.