wppaste
WordPress

home_url( string $path = '', string|null $scheme = null ): string

Since
3.0.0
Source
wp-includes/link-template.php:3441

Builds a front-end URL for the current site using the 'home' option, optionally appending a path and forcing a scheme. It automatically switches to https when is_ssl() is true, unless you pass 'http' or 'https' explicitly to override that check. Use esc_url() when echoing the result inside HTML to avoid unescaped output.

Retrieves the URL for the current 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

$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.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

A theme template wants to build a 'Back to shop' link relative to the site's home URL.

$shop_link = home_url( '/shop/' );

echo esc_html( $shop_link );

home_url() concatenates $path onto the home option as-is, so include the leading slash yourself.

See how the scheme argument changes the returned URL

Compare what home_url() returns for each accepted $scheme value against the same path.

$schemes = array( 'http', 'https', 'relative', 'rest', null );

foreach ( $schemes as $scheme ) {
	$label = null === $scheme ? 'null (default)' : $scheme;
	printf(
		'%s => %s<br>',
		esc_html( $label ),
		esc_html( home_url( '/sample-page/', $scheme ) )
	);
}

Passing 'http' or 'https' overrides whatever is_ssl() would otherwise decide.

Common problems and fixes · 4

Why does home_url() return a different domain than I expected?

home_url() reads the 'home' option, not the actual domain the site is currently being viewed on. If the 'home' option was set for a different domain (a staging copy, a migrated install), the returned URL still points there.

Why is my URL http:// even though the page is loaded over https?

The source shows the protocol only switches to 'https' when is_ssl() evaluates to true; if the front end is behind a proxy or load balancer that terminates SSL before reaching WordPress, is_ssl() can return false and home_url() falls back to the 'home' option's scheme.

Do I need to escape the output of home_url()?

Yes. home_url() returns a plain string built from the 'home' option and whatever $path you pass in; it does not run esc_url() or esc_attr() for you.

What's the difference between home_url() and site_url()?

home_url() is based on the 'home' option (where visitors see the front end) while site_url() is based on the 'siteurl' option (where WordPress core files live). On installs where WordPress is in a subdirectory but the site is served from the root, these two differ.

Alternatives and related functions

get_home_url
When you need a URL for a specific site on a multisite network rather than the current one, since home_url() always passes null as the blog ID.
site_url
When the URL needs to point at WordPress core files (login, admin-ajax.php) rather than the front end visitors see.
admin_url
When building a link into wp-admin, since that appends the /wp-admin/ path onto the site URL for you.
content_url
When linking to a file inside wp-content rather than to a front-end page or post.

Performance profile

How much work a call to 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
8

Executed per call on PHP 8.5. The body compiles to 8.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 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()one call below home_url()
  • hookthird-party callbacksapply_filters()one call below home_url()

Further down the call graph this can also reach cache, serialize, 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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always8get_home_url()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 8 instructions, 8 executed per call, 0 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.

Uses · 1

  • get_home_url()Retrieves the URL for a given site where the front end is accessible.

Used by · 50

Show all 50

Source code

function home_url( $path = '', $scheme = null ) {	return get_home_url( null, $path, $scheme );}

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.0.4 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.