Using WordPress ‘get_home_url()’ PHP function

The get_home_url() WordPress PHP function retrieves the URL for a given site where the front end is accessible.

Usage

get_home_url( int $blog_id = null, string $path = '', string $scheme = null );

Example:
Input:

echo get_home_url();

Output:

https://example.com

Parameters

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

More information

See WordPress Developer Resources: get_home_url

Examples

Get home URL with a custom path

This example gets the home URL and appends the custom path /blog.

$home_url = get_home_url( null, '/blog' );
echo $home_url;

Get home URL with an HTTPS scheme

This example forces the HTTPS scheme for the home URL.

$home_url = get_home_url( null, '', 'https' );
echo $home_url;

Get home URL with a REST scheme

This example gets the home URL with a REST scheme.

$home_url = get_home_url( null, '', 'rest' );
echo $home_url;

Get home URL of a different site in a multisite installation

This example gets the home URL of a different site (with ID 2) in a multisite installation.

$home_url = get_home_url( 2 );
echo $home_url;

Get home URL with a relative scheme

This example gets the home URL with a relative scheme.

$home_url = get_home_url( null, '', 'relative' );
echo $home_url;