home_url vs site_url : differences and usages.
home_url and site_url are the two WordPress functions that you may not find differences when you retrieve the home page address.
In my case, I used both functions to get the site URL and the output was the same.
And I asked myself many questions about home_url vs site_url.
Some of the questions puzzled my head are.
- Why home_url and site_url return the same home address?
- What are the main differences between home_url and site_url?
- When to use home_url over site_url and vice versa?
- Which is a suitable function to retrieve the home page?
But I figured out these functions are not the same.
How are they different?
In fact, site_url retrieves the home URL of the directory where the WordPress application files and folders reside, and while home_url retrieves the address that has been set accessible to a front end user (Blog visitor).
WordPress allows entering the address that is different from the address of the WordPress installation directory.
The setting takes in the general setting tab in the WordPress admin dashboard.
Key Differences home_url & site_url
# | site_url() | home_url() |
---|---|---|
1 | loads home page URL using get_site_url() | loads home page URL using get_home_url() |
2 | Return the address of the WordPress installation directory | Return the address that is accessible to a user on the address bar |
3 | If the WordPress directory is blog2, site_url returns domain.com/blog2 | The home_url returns domain.com |
4 | In admin general setting site_url address is set in WordPress Address (URL) label | site_url address is set in Site Address (URL) label |
Why home_url and site_url return the same home address?
If you install WordPress by one-click install, it sets the root address on both WordPress Address (URL) and Site Address (URL) fields.
For example, if your domain is mydomain.com, the one-click install will install WordPress files and folders in the root directory.
This configures site_url() = mydomain.com and home_url() = mydomain.com.
Hence, when you echo site_url() or home_url(), the home page will be mydomain.com.
But when you install the WordPress files in the sub-directory then you can configure these two functions differently.
How to configure site_url and home_url
The setting of the home page links is done in the WordPress admin dashboard.
Under the Settings tab, select General.
This brings a general setting page where you can alter some default settings.
The settings to be changed are
- WordPress Address (URL)
- Site Address (URL)
WordPress Address (URL) field sets the site url for site_url() function.
Site Address (URL) field sets the site url for home_url() function.
In this image, the WordPress address and site address are set to http://wepappguides.com.
So, whether I call site_url or home_url the output is http://wepappguides.com.
That is why the two function returns the same home URL address.
When to use site_url over home_url?
home_url returns the address that a user types on the browser address bar.
Therefore, the home_url function will always be used to retrieve the site URL.
But the question is, when can site_url be used?
It is when you host multiple WordPress sites on the web host.
This makes the root directory to host many WordPress directories.
When the user of your theme will have this situation, the site_url should be used to retrieve the WordPress files installation address.
Which is a suitable function for the site URL?
home_url, as it is an address that designed for a user to visit the blog.
why?
To me, home_url can retrieve the same site url that is returned by site_url if no changes were made in the site address settings.
But also, home_url can return a different home page URL.
And the home_url protects the installation directories from attacks since a website visitor won’t get permission to access the WordPress directory address.
What to do if site_url or home_url is not working?
It happens when site_url or home_url fails to return the home page of the blog.
I cannot exactly guess what the causes of the issue, but the common problem I saw from others is improper usage of the functions.
It is an issue that I have gone through many times.
For example, calling home_url or site_url without echoing won’t output the home link on the HTML page.
Check this question in WordPress StackExchange to know what I mean.
To solve such a problem just call home_url like this …
<?php echo home_url();?>
And for a site url
<?php echo site_url();?>
Wrap-up
The short description of this post has shown the key differences of home_url vs site_url but I have covered these two functions in many details on this post WordPress functions for site url.
But if the WordPress address settings use the same domain, both functions return the same output otherwise the addresses for the home page.
site_url is most useful when you host many WordPress directories in the same web host but the home_url is recommended if you won’t have many WordPress directories.