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="https://wordpress.stackexchange.com/questions/224062/<?php echo esc_url( home_url() ); ?>/example">Example Page</a>
<a href="<?php echo esc_url( home_url("https://wordpress.stackexchange.com/") ); ?>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="https://wordpress.stackexchange.com/questions/224062/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 Answer
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')
.