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
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
- Scaling
- Constant
- Instructions
- 27–50
- Plugin surface
- 1 hook
- Called by
- 17
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 57.
Third-party callbacks on 'home_url' run inside this call, and their cost is not bounded by anything here.
17 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below get_home_url() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($blog_id) && $scheme | 27–29 | get_option(), set_url_scheme(), apply_filters() |
!empty($blog_id) && !is_multisite() && $scheme | 30–32 | is_multisite(), get_option(), set_url_scheme(), apply_filters() |
empty($blog_id) && !$scheme && is_ssl() | 32–34 | get_option(), is_ssl(), set_url_scheme(), apply_filters() |
!empty($blog_id) && is_multisite() && $scheme | 34–36 | is_multisite(), switch_to_blog(), get_option(), restore_current_blog(), set_url_scheme(), apply_filters() |
empty($blog_id) && $scheme && is_string($path) | 35 | get_option(), set_url_scheme(), ltrim(), apply_filters() |
empty($blog_id) && !$scheme && !is_ssl() | 35–37 | get_option(), is_ssl(), parse_url(), set_url_scheme(), apply_filters() |
!empty($blog_id) && !is_multisite() && !$scheme && is_ssl() | 35–37 | is_multisite(), get_option(), is_ssl(), set_url_scheme(), apply_filters() |
!empty($blog_id) && !is_multisite() && $scheme && is_string($path) | 38 | is_multisite(), get_option(), set_url_scheme(), ltrim(), apply_filters() |
!empty($blog_id) && !is_multisite() && !$scheme && !is_ssl() | 38–40 | is_multisite(), get_option(), is_ssl(), parse_url(), set_url_scheme(), apply_filters() |
!empty($blog_id) && is_multisite() && !$scheme && is_ssl() | 39–41 | is_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) | 40 | get_option(), is_ssl(), set_url_scheme(), ltrim(), apply_filters() |
!empty($blog_id) && is_multisite() && $scheme && is_string($path) | 42 | is_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–44 | is_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) | 43 | get_option(), is_ssl(), parse_url(), set_url_scheme(), ltrim(), apply_filters() |
!empty($blog_id) && !is_multisite() && !$scheme && is_ssl() && is_string($path) | 43 | is_multisite(), get_option(), is_ssl(), set_url_scheme(), ltrim(), apply_filters() |
!empty($blog_id) && !is_multisite() && !$scheme && !is_ssl() && is_string($path) | 46 | is_multisite(), get_option(), is_ssl(), parse_url(), set_url_scheme(), ltrim(), apply_filters() |
!empty($blog_id) && is_multisite() && !$scheme && is_ssl() && is_string($path) | 47 | is_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) | 50 | is_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:
Uses · 7
- is_multisite()Determines whether Multisite is enabled.
- get_option()Retrieves an option value based on an option name.
- switch_to_blog()Switches the current blog.
- restore_current_blog()Restores the current blog, after calling switch_to_blog().
- is_ssl()Determines if SSL is used.
- set_url_scheme()Sets the scheme for a URL.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 17
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_MS_Sites_List_Table::handle_row_actions()Generates and displays row action links.
- WP_MS_Users_List_Table::column_blogs()Handles the sites column output.
- _access_denied_splash()Displays an access denied message when a user tries to view a site's dashboard they do not have access to.
- choose_primary_blog()Handles the display of choosing a user's primary site.
- confirm_delete_users()
- get_oembed_response_data()Retrieves the oEmbed response data for a given post.
- get_post_type_archive_link()Retrieves the permalink for a post type archive.
- get_rest_url()Retrieves the URL to a REST endpoint on a site.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- install_blog()Install an empty blog.
- options_general_add_js()Display JavaScript on the page.
Show all 17
- signup_another_blog()Shows a form for returning users to sign up for another site.
- twentytwenty_site_logo()Displays the site logo, either text or image.
- wp_admin_bar_my_sites_menu()Adds the "My Sites/[Site Name]" menu and all submenus.
- wp_admin_bar_site_menu()Adds the "Site Name" menu.
- wp_initialize_site()Runs the initialization routine for a given site.
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.
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.