Using WordPress ‘home_url()’ PHP function

The home_url() WordPress PHP function retrieves the URL for the current site where the front end is accessible.

Usage

home_url($path = '', $scheme = null);
  • Custom example:
    • Input: home_url('/blog', 'https')
    • Output: https://www.example.com/blog

Parameters

  • $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: home_url

Examples

Basic home URL

This example retrieves the basic home URL without a trailing slash.

$url = home_url();
echo $url; // Output: http://www.example.com

Home URL with trailing slash

This example retrieves the home URL with a trailing slash.

$url = home_url('/');
echo $url; // Output: http://www.example.com/

Home URL with HTTPS scheme

This example retrieves the home URL with the HTTPS scheme.

$url = home_url('/', 'https');
echo $url; // Output: https://www.example.com/

Home URL with a relative path

This example retrieves the home URL with a relative path.

$url = home_url('example', 'relative');
echo $url; // Output: /example

Home URL with additional attributes

This example retrieves the home URL with additional attributes added to the path.

$url = esc_url(home_url('/my-page?id=123'));
echo $url; // Output: https://your-domain/my-page?id=123