Using WordPress ‘get_site_url()’ PHP function

The get_site_url() WordPress PHP function retrieves the URL for a given site where WordPress application files are accessible.

Usage

get_site_url($blog_id, $path, $scheme)

Custom example:

echo get_site_url(1, 'my-page', 'https');

Output: https://www.example.com/my-page

Parameters

  • $blog_id (int|null) – Optional. Site ID. Default null (current site).
  • $path (string) – Optional. Path relative to the site URL. Default: ”.
  • $scheme (string|null) – Optional. Scheme to give the site URL context. Accepts ‘http’, ‘https’, ‘login’, ‘login_post’, ‘admin’, or ‘relative’. Default: null.

More information

See WordPress Developer Resources: get_site_url()

Examples

Basic usage

Display the full site URL.

echo get_site_url();

Output: http://www.example.com

Get the hostname/domain component

To get just the hostname/domain component of the Site URL (without paths, schema, etc.).

echo parse_url(get_site_url(), PHP_URL_HOST);

WordPress installed in a subfolder

If WordPress is installed into a subfolder, this example shows the URL with the subfolder.

echo get_site_url();

Output: https://www.example.com/sub/folder

Add a custom path

Get the site URL with a custom path.

echo get_site_url(null, 'my-page');

Output: http://www.example.com/my-page

Specify a scheme

Get the site URL with a specified scheme.

$url = get_site_url(null, '/wp-content/themes/', 'https');
echo $url;

Output: https://www.example.com/wp-content/themes/