home_url()WP 3.0.0

Gets the URL of the site's main page (without the trailing slash /). Considers the protocol (http, https).

Returns the value of the option get_option('home') with the correct protocol. The protocol will be:

  • https — when the condition is_ssl() is met.
  • http — in other cases.

The protocol can be forcibly rewritten by specifying the second parameter $scheme.

Use this function when you need to get the site URL (front), not the WordPress URL (admin) (see General Settings).

Use site_url() when you need to get the WordPress URL (admin, where the files are located).

The constant WP_HOME - in wp-config.php you can specify the constant WP_HOME, then this function will get the value of this constant, not the value from the database.

For multisite. Use get_home_url( $blog_id ) when you need to get a link to another site in the network of sites, not the URL of the current site.

1 time — 0.000047 sec (very fast) | 50000 times — 0.51 sec (very fast) | PHP 7.1.2, WP 4.7.3

No Hooks.

Returns

String. The URL of the site.

Usage

home_url( $path, $scheme );
$path(string)
The path that will be appended to the end of the link.
Default: ''
$scheme(string)
The URL protocol. By default, it is calculated via is_ssl(). Can be: http, https, relative.
Default: null

Examples

1

#1 Examples of obtaining a website address:

echo home_url();                       // http://example.com

echo home_url('/');                    // http://example.com/

echo home_url( 'blog', 'relative' );   // /blog

echo home_url( 'blog' );               // https://example.com/blog

echo home_url( '/blog', 'https' );     // https://example.com/blog

echo home_url( '#hash', 'https' );     // https://example.com/#hash

echo home_url( '//foo.bar/foo' );      // http://example.com/foo.bar/foo

echo home_url( 'http://foo.bar/foo' ); // http://example.com/http://foo.bar/foo

echo home_url( '/mypage?id=123' );     // https://example.com/mypage?id=123
0

#2 If WP core is installed to sub directory wp

echo home_url(); // https://example.com
echo site_url(); // https://example.com/wp

Changelog

Since 3.0.0 Introduced.

home_url() code WP 7.0

function home_url( $path = '', $scheme = null ) {
	return get_home_url( null, $path, $scheme );
}