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.
Returns the site_url with the appropriate protocol, 'https' if is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is overridden.
No Hooks.
Return
String
. Site URL link with optional path appended.
Usage
site_url( $path, $scheme );
- $path(string)
- Path relative to the site URL.
Default: '' - $scheme(string|null)
- Scheme to give the site URL context. See set_url_scheme().
Default: null
Examples
#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)
#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
#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
#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() site url code WP 6.7.1
function site_url( $path = '', $scheme = null ) { return get_site_url( null, $path, $scheme ); }