How to get site URL in WordPress
This article provides a comprehensive guide on retrieving the site URL and WordPress URL, highlighting the differences between these two essential configurations. Exploring the distinctions between these URLs, the article will delve into the core functions, such as get_site_url()
and get_home_url()
, used to access and differentiate between these vital URLs within WordPress.
Difference between site URL and WordPress URL
Both Site and WordPress address options can be configured in Settings -> General section of the WordPress dashboard.
The Site URL in WordPress refers to the web address or location where the WordPress site is accessible on the internet. It represents the base URL used to access the site’s homepage and its various pages and content. This configuration is stored in the wp_options
table under the “home” key.
The WordPress URL designates the location where the core WordPress files are stored. Typically, this URL is the same as the Site URL. However, this URL might differ in some cases, particularly if the WordPress installation isn’t installed in the root directory but resides within a subdirectory. This configuration is stored in the wp_options
table under the “siteurl” key.
How to get the Site URL in WordPress
As illustrated in the screenshot above, our Site URL is set to https://example.com. This is the actual website URL that is served on the internet. In this section, we will explore the different methods to retrieve this URL.
Gain Valuable Insights From Your WordPress User Data
Using the get_bloginfo() function
You can retrieve the site URL by using the get_bloginfo()
function with ‘url’ passed as parameter:
get_bloginfo('url'); // https://example.com
Using the home_url() function
Another way to retrieve the site URL is by using the home_url()
function. If no parameters are passed, the function will return the home page URL of the website.
home_url(); // https://example.com
Optionally, you can pass a path as parameter. This will return the URL for the given path relative to the site’s homepage URL:
home_url('shop'); // https://example.com/shop
Using the get_home_url() function
The get_home_url()
function can be also used to retrieve the site URL. When used without any parameters, it returns the website’s home page.
get_home_url(); // https://example.com
This function can be also used on multi-site installations to retrieve the URL for other network sites by passing the site ID:
$blog_id = 3; get_home_url($blog_id);
If the $blog_id
parameter is omitted, the function will return the Site URL for the current site.
How to get the WordPress URL
Following the example on the screenshot above, we have set our WordPress URL to https://example.com/wp-files. This is where the WordPress core files reside.
Similarly, there are a few different ways to retrieve this URL:
Using the get_bloginfo() function
You can retrieve the WordPress URL by using the get_bloginfo()
function with ‘wpurl’ passed as parameter:
get_bloginfo('wpurl'); // https://example.com/wp-files
Using the site_url() function
The WordPress core files URL can be also retrieved by using the site_url()
function. When no parameters are passed, the function will return the URL for the current site where WordPress PHP files reside.
site_url(); // https://example.com/wp-files
Optionally, you can pass a path as parameter. This will return the URL for the given path relative to the WordPress URL.
site_url('wp-content'); // https://example.com/wp-files/wp-content
Using the get_site_url() function
The get_site_url()
function can also be used to retrieve the WordPress URL. When used without any parameters, it works like the site_url()
function:
get_site_url(); // https://example.com/wp-files
This function can also be used on multi-site installations to retrieve the WordPress core files URL for other network sites by passing the site ID as a parameter:
$blog_id = 3; get_site_url($blog_id);
If the $blog_id
parameter is omitted, the function will return the URL for the current site.
Conclusion
Understanding the difference between the Site URL and WordPress URL is crucial in WordPress work. This article explained these basic settings and their contrasts in WordPress. It explored different ways to get these URLs, helping you understand how they work. Learning how to use functions like get_site_url() and get_home_url() can give you specific tools to get these URLs accurately in WordPress projects.