WordPress Code Snippets

A fresh mix of code snippets and tutorials

Difference between home_url & site_url

270

Here are some notes to help discern the subtle (and oft-confusing) differences between the WordPress functions, home_url and site_url (and their related “get” functions, get_home_url and get_site_url). We’ll look at the output of these functions and how they relate to the WordPress General Settings.

tl;dr

Summary of this article:

home_url()     = https://example.com
get_home_url() = https://example.com
site_url()     = https://example.com/wordpress
get_site_url() = https://example.com/wordpress

In a nutshell, appending /wp-admin/ to site_url() always gives the location of the WP Admin Area. This may or may not be the case for home_url(), which frequently corresponds to the site’s root directory (but not always).

DIY

For quick, specific results, add the following code to your WordPress theme or plugin:

echo '<ul>';
echo '<li>home_url()     = '. home_url()     . '</li>';
echo '<li>get_home_url() = '. get_home_url() . '</li>';
echo '<li>site_url()     = '. site_url()     . '</li>';
echo '<li>get_site_url() = '. get_site_url() . '</li>';
echo '</ul>';

And/or read on to dig deeper into these similar WordPress functions..

General Settings

Here are the two General Settings associated with home_url() and site_url() (and their related “get” functions):

  • Site Address (URL) = home_url() = https://example.com
  • WordPress Address (URL) = site_url() = https://example.com/wordpress

Thus, the frequent confusion between home_url() and site_url() happens because the WP General Settings uses terminology that does not match up with the associated template tags. I.e., site_url() corresponds to “WordPress Address”, while home_url() corresponds to “Site Address”. This seems backwards, at best.

home_url()

home_url() returns the “Site Address”, as defined in the General Settings.

Syntax: <?php home_url($path, $scheme); ?>

Example: home_url() = https://example.com

References:

get_home_url()

get_home_url() returns the “Site Address”, as defined in the General Settings. Note that this function accepts an optional $blog_id parameter, which makes it useful for Multisite setups.

Syntax: <?php get_home_url($blog_id, $path, $scheme); ?>

Example: get_home_url() = https://example.com

References:

site_url()

site_url() returns the “WordPress Address”, as defined in the General Settings.

Syntax: <?php site_url($path, $scheme); ?>

Example: site_url() = https://example.com/wordpress

References:

get_site_url()

get_site_url() returns the “WordPress Address”, as defined in the General Settings. Note that this function accepts an optional $blog_id parameter, which makes it useful for Multisite setups.

Syntax: <?php get_site_url($blog_id, $path, $scheme); ?>

Example: get_site_url() = https://example.com/wordpress

References:

Learn more

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!