My understanding is that site_url()
returns the location where the WordPress core files are.
If my blog is hosted at http://example.com/blog
then site_url()
returns http://example.com/blog
But then how does home_url()
differ? For me, home_url()
returns the same thing: http://example.com/blog
If that’s correct, then can I get WordPress to return http://example.com/
?
You are asking two questions at once:
home_url()
andsite_url()
?Here are the answers, and I confirmed with Andrew Nacin, a core developer of WordPress, as well as ran some server tests to confirm what Andrew told me.
Question # 1
In General > Settings of wp-admin,
home_url()
references the field labeled “Site Address (URL)”. Confusing, huh? Yeah, it says “Site Address” so you might assumesite_url()
, but you’d be wrong. Run your own test and you’ll see. (You can temporarily drop anecho H1
field withsite_url()
andhome_url()
values at the top of your your theme’s functions.php.)Meanwhile,
site_url()
references the field labeled “WordPress Address (URL)” in General > Settings.So, if you’re wanting to reference where a physical path might be such as calling a plugin’s folder path on the URL to load an image, or calling a theme’s folder path to load an image, you should actually use other functions for those – look at
plugins_url()
andget_template_directory_uri()
.The
site_url()
will always be the location where you can reach the site by tacking on/wp-admin
on the end, whilehome_url()
would not reliably be this location.The
home_url()
would be where you have set your homepage by setting General > Settings “Site Address (URL)” field.Question # 2
So, if I have placed my blog in
http://example.com/blog
, andexample.com
is just some static site where I have like a portfolio theme, then this would be a scenario that lines up with your question. In such a case, then I would use this snippet of code:##TLDR:
In a non-standard installation, you can place your WordPress files in a subdirectory of your website root.
…and still allow your visitors to visitors to access your WordPress “website” from your site’s Domain (root) URL, without appending the subdirectory name:
(ie:
www.example.com
vswww.example.com/wordpress
):Where the value for a WP constant takes precedence over an wp_options/ WP Settings value.
##Different Configurations for WordPress
In the most standard WordPress installations,
home_url
andsite_url
will have the same value.Regardless, they represent two different things.
In a non-standard installation, they may have different values.
(
//
is therelative
protocol and will work for either/bothhttp://
orhttps://
)Standard Installations (including “One-Click” Installs)
home_url
: is the home page of your (wordpress) website, as indicated in the user’s address bar.site_url
: is the directory where your wordpress files are located.WordPress’s 5-minute install installs wordpress files these two values will be the same – wordpress files will be installed in the same folder that you want people to use to address your website, or the wordpress (blog) portion of your server’s website.
Example 1:
user accesses your blog at:
www.example.com
,wordpress files installed at:
www.example.com
, or the root folder of your server’s website.home_url
===site_url
==="www.example.com"
Example 2:
user accesses your blog at:
www.example.com/blog
,wordpress files installed at:
www.example.com/blog
, or in theblog
folder inside the root of your website.home_url
===site_url
==="www.example.com/blog"
In this case
www.example.com
is the main website, andwww.example.com/blog
is the root of your blog.Here your blog is separated from, and works as subset of, your main website.
In this case, your main website is not controlled, defined, or styled by WordPress.
Just your blog is. All urls in your blog will be proceeded by
www.example.com/blog
Note: In documentation, “WordPress site/website” (as opposed to simply “site/website”) refers to the directory where your WordPress files are installed. In this case, it is
www.example.com/blog
– everything within theblog
folder. The “WordPress website”, in this scenario, is not the same as your domain, your root, or your main website. It is a subset of your overall website. Kind of like a website inside a website. I mention this as the terminology can seem unclear or confusing, given this particular setup.Alternate WordPress Installation Configuration
Giving WordPress Its Own Directory, the section
Method II (With URL change)
.For example, many people don’t want to clog up the root folder of their website with all the wordpress files.
They want to install wordpress in a subdirectory, *but have the “blog” or “WordPress website” accessed as if the files were installed in the root of the server’s root for the website.
This is particularly be true when WordPress is used to build and run an entire website that does not even have a “blog”.
Example 3:
user accesses your “blog” at:
www.example.com
,wordpress files installed at:
www.example.com/wordpress
, or the root folder of your server’s website.home_url
==="www.example.com"
site_url
==="www.example.com/wordpress"
(Note: this configuration will not work “out of the box” just by changing the values of these variables. It requires additional configuration changes to work properly)
See Giving WordPress Its Own Directory, the section titled
Method II (With URL change)
for how to do this.In this case
home_url
andsite_url
should hold different values.In this setup, you want your website to function exactly as if WordPress files were installed in the server’s root directory for your website…
BUT, for organizational purposes on the server,
you actually have your WordPress files in a folder called
wordpress
in the server’s root directory for your website.So, user will type in
www.example.com
to get your WordPress home page, instead ofwww.example.com/wordpress
wordpress function <–> database variable <–> WordPress Constant
(The other cases are trivial: All variables/functions hold/return the same value.)
How to set the values for
site_url
andhome_url
First, let me note that
siteurl
andhome
store values returned by the functions aboveSettings -> General ->
siteurl
WordPress Address:https://www.example.com/wordpress
home
Site Address:https://www.example.com
(do not include trailing slashes here – that would be configured elsewhere)
Alternatively, you set these values in your WordPress database:
wp_options
table ->options_name
|options_value
siteurl
|https://www.example.com/wordpress
home
|https://www.example.com
(do not include trailing slashes here – that would be configured elsewhere)
Edit your
wp-config.php
Define these specific constants to hold your values
Define
WP_HOME
andWP_SITEURL
settings by inserting these lines toward the top of yourwp-config.php
file:define(‘WP_SITEURL’,’http://example.com/wordpress’); // wordpress core files
define(‘WP_HOME’,’http://example.com’); // address bar url
// ** MySQL settings – You can get this info from your web host ** //
…
(do not include trailing slashes here – that would be configured elsewhere)
Reference: WP_SITEURL and WP_HOME
NOTE: This is confusing
(I really wish WordPress had Labeled the Settings similar to their php names,
such as
WordPress Site Address
andHome Page Address
or something more explicit likelocation of WordPress Site core files
andbrowser url to access WordPress home page
)Now Here is where it gets tricky !
IF you defined those constants in your
wp-config.php
file, it does not matter what values you have in your database/settings page.In fact, you will not be able to modify this value through the back end (it’ll be greyed out). You can still modify in by editing your database, but doing so will have no effect on your site, while the constants exist in your wp-config file.
You config file will not change the values in your database (or hence you settings page). Instead, your database/settings page values will be ignored. The values in wp-config override or take precedence over your database setting.
#So… to wrap up (TLDR) :
Where the value for a WP constant takes precedence over an wp_options/ WP Settings value.
The wp_options record value and the WP Settings value are the same.
Editing one, by definition edits the other.
It is just 2 different ways of accessing the same variable.
On the other hand, the WordPress Constants are unique and independent.
Internally, WordPress (PHP) constants override their db counterparts.
If a constant is defined in wp-config, it does not change the database.
But internally WordPress will always prefer/use its value instead of the db one.
If you want WP installed in a directory but the site home on your domain root, you need to move the main index.php file out to your domain root and edit the require statement to point within your directory.
This process is outlined here: Giving WordPress Its Own Directory.
The
site_url()
andhome_url()
functions are similar and can lead to confusion in how they work.The
site_url()
function retrieves the value value forsiteurl
in thewp_options
table in your database.This is the URL to the WordPress core files.
If your core files exist in a subdirectory
/wordpress
on your web server, the value would behttp://example.com/wordpress
.The
home_url()
function retrieves the value forhome
in thewp_options
table in your database.This is the address you want people to visit to view your WordPress web site.
If your WordPress core files exist in
/wordpress
, but you want your web site URL to behttp://example.com
the home value should behttp://example.com
.To answer your second question:
You can’t, unless you take the Giving WordPress its own directory steps. Using this means you put WordPress core files into
/blog
or/WordPress
and then theindex.php
into your root.If you decide to put WordPress inside its own directory then you would use
home_url()
for going toindex.php
andsite_url()
for getting core files and such.Refrences:
Codex for
site_url
Codex for
home_url
Codex for Giving WordPress Own Directory
The easiest way to get the site url without any sub-directories ( http://example.com/ instead of http://example.com/blog ), just use the backslash /
For example, if you type:
It will create a link that goes to your domain