wppaste
WordPress

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

Since
3.0.0
Source
wp-includes/link-template.php:3460
Retrieves the URL for a given site where the front end is accessible.

Description

Returns the 'home' option with the appropriate protocol. The protocol will be 'https' if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
If $scheme is 'http' or 'https', is_ssl() is overridden.

Compatibility

WordPress
since 3.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$blog_idint|nulloptional
Site ID. Default null (current site).Default: null
$pathstringoptional
Path relative to the home URL. Default empty.Default: ''
$schemestring|nulloptional
Scheme to give the home URL context. Accepts 'http', 'https', 'relative', 'rest', or null. Default null.Default: null

Return value

string
Home URL link with optional path appended.

Performance profile

How much work a call to get_home_url() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
27–50

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 57.

Plugin surface
1 hook

Third-party callbacks on 'home_url' run inside this call, and their cost is not bounded by anything here.

Called by
17

17 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below get_home_url()
  • serializeserialisationmaybe_unserialize()one call below get_home_url()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 18 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_home_url() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($blog_id) && $scheme27–29get_option(), set_url_scheme(), apply_filters()
!empty($blog_id) && !is_multisite() && $scheme30–32is_multisite(), get_option(), set_url_scheme(), apply_filters()
empty($blog_id) && !$scheme && is_ssl()32–34get_option(), is_ssl(), set_url_scheme(), apply_filters()
!empty($blog_id) && is_multisite() && $scheme34–36is_multisite(), switch_to_blog(), get_option(), restore_current_blog(), set_url_scheme(), apply_filters()
empty($blog_id) && $scheme && is_string($path)35get_option(), set_url_scheme(), ltrim(), apply_filters()
empty($blog_id) && !$scheme && !is_ssl()35–37get_option(), is_ssl(), parse_url(), set_url_scheme(), apply_filters()
!empty($blog_id) && !is_multisite() && !$scheme && is_ssl()35–37is_multisite(), get_option(), is_ssl(), set_url_scheme(), apply_filters()
!empty($blog_id) && !is_multisite() && $scheme && is_string($path)38is_multisite(), get_option(), set_url_scheme(), ltrim(), apply_filters()
!empty($blog_id) && !is_multisite() && !$scheme && !is_ssl()38–40is_multisite(), get_option(), is_ssl(), parse_url(), set_url_scheme(), apply_filters()
!empty($blog_id) && is_multisite() && !$scheme && is_ssl()39–41is_multisite(), switch_to_blog(), get_option(), restore_current_blog(), is_ssl(), set_url_scheme(), apply_filters()
empty($blog_id) && !$scheme && is_ssl() && is_string($path)40get_option(), is_ssl(), set_url_scheme(), ltrim(), apply_filters()
!empty($blog_id) && is_multisite() && $scheme && is_string($path)42is_multisite(), switch_to_blog(), get_option(), restore_current_blog(), set_url_scheme(), ltrim(), apply_filters()
6 further outcomes, up to 50 instructions
!empty($blog_id) && is_multisite() && !$scheme && !is_ssl()42–44is_multisite(), switch_to_blog(), get_option(), restore_current_blog(), is_ssl(), parse_url(), set_url_scheme(), apply_filters()
empty($blog_id) && !$scheme && !is_ssl() && is_string($path)43get_option(), is_ssl(), parse_url(), set_url_scheme(), ltrim(), apply_filters()
!empty($blog_id) && !is_multisite() && !$scheme && is_ssl() && is_string($path)43is_multisite(), get_option(), is_ssl(), set_url_scheme(), ltrim(), apply_filters()
!empty($blog_id) && !is_multisite() && !$scheme && !is_ssl() && is_string($path)46is_multisite(), get_option(), is_ssl(), parse_url(), set_url_scheme(), ltrim(), apply_filters()
!empty($blog_id) && is_multisite() && !$scheme && is_ssl() && is_string($path)47is_multisite(), switch_to_blog(), get_option(), restore_current_blog(), is_ssl(), set_url_scheme(), ltrim(), apply_filters()
!empty($blog_id) && is_multisite() && !$scheme && !is_ssl() && is_string($path)50is_multisite(), switch_to_blog(), get_option(), restore_current_blog(), is_ssl(), parse_url(), set_url_scheme(), ltrim(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 57 instructions, 27–50 executed per call, 6 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_home_url() runs, in this order:

  1. apply_filters( home_url )filterline 3496 (+36 into the body)

    Filters the home URL.

Uses · 7

Used by · 17

Show all 17

Source code

function get_home_url( $blog_id = null, $path = '', $scheme = null ) {	$orig_scheme = $scheme; 	if ( empty( $blog_id ) || ! is_multisite() ) {		$url = get_option( 'home' );	} else {		switch_to_blog( $blog_id );		$url = get_option( 'home' );		restore_current_blog();	} 	if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) {		if ( is_ssl() ) {			$scheme = 'https';		} else {			$scheme = parse_url( $url, PHP_URL_SCHEME );		}	} 	$url = set_url_scheme( $url, $scheme ); 	if ( $path && is_string( $path ) ) {		$url .= '/' . ltrim( $path, '/' );	} 	/**	 * Filters the home URL.	 *	 * @since 3.0.0	 *	 * @param string      $url         The complete home URL including scheme and path.	 * @param string      $path        Path relative to the home URL. Blank string if no path is specified.	 * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',	 *                                 'relative', 'rest', or null.	 * @param int|null    $blog_id     Site ID, or null for the current site.	 */	return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );}

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.