Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My understanding is that site_url() returns the location where the wordpress core files are.

So, 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/ ?

share|improve this question
3  
You're asking two questions at once on a very important question. The answer to "What's the difference between home_url() and site_url()?" is different than the question, "How do I get WordPress to return the URL root without the subdirectory where it's installed?" –  Volomike Apr 29 '12 at 23:33

4 Answers 4

up vote 12 down vote accepted

You are asking two questions at once:

  1. What's the difference between home_url() and site_url()?
  2. How do I get WordPress to return the URL root without the subdirectory where it's installed?

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 assume site_url(), but you'd be wrong. Run your own test and you'll see. (You can temporarily drop an echo H1 field with site_url() and home_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() and get_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, while home_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, and example.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:

<?php
function getDomain() {
    $sURL    = site_url(); // WordPress function
    $asParts = parse_url( $sURL ); // PHP function

    if ( ! $asParts )
      wp_die( 'ERROR: Path corrupt for parsing.' ); // replace this with a better error result

    $sScheme = $asParts['scheme'];
    $nPort   = $asParts['port'];
    $sHost   = $asParts['host'];
    $nPort   = 80 == $nPort ? '' : $nPort;
    $nPort   = 'https' == $sScheme AND 443 == $nPort ? '' : $nPort;
    $sPort   = ! empty( $sPort ) ? ":$nPort" : '';
    $sReturn = $sScheme . '://' . $sHost . $sPort;

    return $sReturn;
}
share|improve this answer
    
Do you have a link to the discussion with A.Nacin? –  kaiser Apr 30 '12 at 1:02
1  
It was via email. Sorry. Oh, and thanks for the edit -- I'll remember that syntax change next time. –  Volomike Apr 30 '12 at 2:07
1  
It took me a very long time and a lot of pain to realize that 'Site Address (URL)' = 'home' and 'WordPress Address (URL)' = 'siteurl'. They should definitely change those labels. –  Jbm Jan 26 at 3:33

The site_url() and home_url() functions are similar and can lead to confusion in how they work. The site_url() function retrieves the value as set in the wp_options table value for siteurl 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 be http://example.com/wordpress. The home_url() function retrieves the value for home in the wp_options table. 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 be http://example.com the home value should be http://example.com.

share|improve this answer

To answer you second question:

Q: If that's correct, then can I get wordpress to return http://example.com/ ?

Not unless you use the giving WordPress it own directory. Using this means you put WordPress core files into /blog or /WordPress and then the index.php into your root

If you decide to put WordPress inside its own directory then you would use home_url() for going to index.php and site_url() for getting core files and such.

Refrences:
Codex for site_url
Codex for home_url
Codex for Giving Wordpress Own Directory

share|improve this answer

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.

share|improve this answer
    
I always just use home_url() since i am on wp network mode. I have only given WordPress its own directory once and it just was not my liking. But i do however use the wp_content_dir on some sites. –  xLRDxREVENGEx Jun 17 '11 at 18:29
    
I don't have any experience with multisite, so I'm not familiar how this stuff works in that situation. I prefer installing WP in a directory just to keep things clean and not clutter the root. –  Milo Jun 17 '11 at 18:34
    
my file structure is probably one of the neatest ones around. home/usr/public_html/site1 home/usr/public_html/site2 and so on and then the wp_content_dir is usually on a cdn –  xLRDxREVENGEx Jun 17 '11 at 18:39
    
if a WP install were the only thing there it would be fine, but I'm mostly working on other peoples servers with hundreds of files and directories littered about. –  Milo Jun 17 '11 at 18:46
    
Is my understanding correct that site_url() and home_url() are the same, unless one sets up their wordpress install directory to be different than the root? –  Praveen Jun 20 '11 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.