3

This isn't a problem but rather I am just curious to see how others are using this function. The codex does not really say a preferred method of use although it does provide some straight-forward examples.

For example if my domain is example.com I can write any of these:

<a href="<?php echo esc_url( home_url() ); ?>/example">Example Page</a>
<a href="<?php echo esc_url( home_url('/') ); ?>example">Example Page</a>
<a href="<?php echo esc_url( home_url('/example') ); ?>">Example Page</a>
<a href="<?php echo esc_url( home_url('example') ); ?>">Example Page</a>
<a href="<?php echo esc_url( home_url('example', 'relative') ); ?>">Example Page</a>

And they will all output the same result (simplified) :

<a href="domain.com/example">Example Page</a>

I use the third example given most often and I understand the use of the last example, as at times I need to link to a secure page (https://), but what is the point of the other accepted variations?

Is one method considered a "best practice" or is it just left up to personal preference?

  • 1
    As a theme developer I've only ever used the first example which would link to the homepage, usually the logo or a banner. Any other links in the theme are either generated by the user or WordPress. – Howdy_McGee Apr 18 '16 at 18:35
  • @Howdy_McGee when you say the links are generated by users or WordPress do you mean that they originate from the default content field? I end up using this when links are either needed to be hard coded or generated from an ACF input, and of course as you said for logos and banners – bebaps Apr 18 '16 at 19:03
  • 1
    This is potentially off-topic because it's more opinion based, but I'd say the same as @Howdy_McGee - I've only ever used the first one. Other links either come from the menu system, from content fields, etc. etc. – Tim Malone Apr 19 '16 at 0:09
0

Million ways in WordPress to use home_url() or sister functions. The question to ask is: What function is a wrapper for other function?

To summarize, if you like to concatenate you may use:

get_bloginfo('url');
get_option('home');

If you like to have the control over scheme (http or https)

get_site_url( $blog_id, $path, $scheme );
get_home_url( $blog_id, $path, $scheme );
site_url( $path, $scheme );

Where site_url calls get_site_url:

function site_url( $path = '', $scheme = null ) {
    return get_site_url( null, $path, $scheme );
}

You may like get_bloginfo('url'); or get_bloginfo('wpurl'); since it will return:

    case 'url' :
        $output = home_url();
        break;
    case 'wpurl' :
        $output = site_url();
        break;

The primitive is: get_option('home') Because function get_home_url calls get_option('home'). What I wrote for get_option('home') is similar for get_option('siteurl') that came from get_bloginfo('wpurl').

  • yea, I notice a lot of that with WordPress. I just don't think that it is necessary a lot of the time. – bebaps Apr 20 '16 at 19:21

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.