site_url()WP 3.0.0

Gets the site URL (where the WordPress core is installed) with the corresponding protocol (https, if the is_ssl condition is triggered).

When you have a WordPress network installed, use network_site_url() instead of this function.

By specifying the blog address, we can move all WordPress files to a separate directory. This is done for convenience when the user is bothered by WordPress files in the main directory and would like to move them to a subdirectory of the main directory. How to do this read here.

The constant WP_SITEURL - in wp-config.php you can specify the constant WP_SITEURL, then its value will be taken for this option, not the value from the database.

Use this function when you need to get the "WordPress engine URL," not the "site URL."

Use home_url() when you need to get the site URL (front).

1 time — 0.00026 sec (fast) | 50000 times — 7.44 sec (fast)

No Hooks.

Returns

String. Site URL.

Usage

site_url( $path, $scheme );
$path(string)
Path that should be appended to the end of the obtained link.
Default: ''
$scheme(string)

Scheme according to which the URL should be modified. Can be:

http
https
relative    — returns a relative URL (without the domain).
login       — login page protocol
login_post
admin
rest
rpc
null        — current site protocol is_ssl()

For more details, see set_url_scheme()

Default: null

Examples

1

#1 Demo example of the function:

$url = site_url();
echo $url;

// outputs: http://www.example.com or http://www.example.com/wordpress
// Depends on what is specified in the site settings: General Settings > WordPress Address (URL)
1

#2 Demo of all variants

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

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

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

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

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

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

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

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

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

#3 If WP core is installed to sub directory wp

site_url() retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.

The situation where “/wp/” may occur in the URL arises when WordPress is installed in a sub-directory other than root public_html (or whatever web root your host uses), but wp-content may places in root directory in this case.

This can be useful if you’re trying to achieve that end. If instead, you are trying to reliably access the URL of the home page of the site, be sure to see home_url().

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

#4 What happens if you specify the first parameter:

$url = site_url( '/secrets/', 'https' );
echo $url;

// Returns: 
// https://example.com/secrets/ 

// OR if WP core is installed to sub directory wp
// https://example.com/wp/secrets/

Changelog

Since 3.0.0 Introduced.

site_url() code WP 6.9.1

function site_url( $path = '', $scheme = null ) {
	return get_site_url( null, $path, $scheme );
}