On this page
get_home_url( int|null $blog_id = null, string $path = '', string|null $scheme = null ): string
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.
Parameters
$blog_idint|null Optional-
Site ID. Default null (current site).
Default:
null $pathstring Optional-
Path relative to the home URL.
Default:
'' $schemestring|null Optional-
Scheme to give the home URL context. Accepts
'http','https','relative','rest', or null.Default:
null
Return
string Home URL link with optional path appended.
More Information
Basic Usage
<?php echo get_home_url(); ?>
Will output: https://www.example.com With the domain and the schema matching your settings.
Source
File: wp-includes/link-template.php. View all references
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 );
}
Hooks
Related
Uses
| Uses | Description |
|---|---|
| is_ssl() wp-includes/load.php | Determines if SSL is used. |
| set_url_scheme() wp-includes/link-template.php | Sets the scheme for a URL. |
| switch_to_blog() wp-includes/ms-blogs.php | Switch the current blog. |
| restore_current_blog() wp-includes/ms-blogs.php | Restore the current blog, after calling switch_to_blog() . |
| is_multisite() wp-includes/load.php | If Multisite is enabled. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_option() wp-includes/option.php | Retrieves an option value based on an option name. |
Used By
| Used By | Description |
|---|---|
| wp_initialize_site() wp-includes/ms-site.php | Runs the initialization routine for a given site. |
| get_rest_url() wp-includes/rest-api.php | Retrieves the URL to a REST endpoint on a site. |
| get_oembed_response_data() wp-includes/embed.php | Retrieves the oEmbed response data for a given post. |
| WP_MS_Sites_List_Table::handle_row_actions() wp-admin/includes/class-wp-ms-sites-list-table.php | Generates and displays row action links. |
| WP_MS_Users_List_Table::column_blogs() wp-admin/includes/class-wp-ms-users-list-table.php | Handles the sites column output. |
| signup_another_blog() wp-signup.php | Shows a form for returning users to sign up for another site. |
| _access_denied_splash() wp-admin/includes/ms.php | Displays an access denied message when a user tries to view a site’s dashboard they do not have access to. |
| choose_primary_blog() wp-admin/includes/ms.php | Handles the display of choosing a user’s primary site. |
| options_general_add_js() wp-admin/includes/options.php | Display JavaScript on the page. |
| confirm_delete_users() wp-admin/includes/ms.php | |
| home_url() wp-includes/link-template.php | Retrieves the URL for the current site where the front end is accessible. |
| get_post_type_archive_link() wp-includes/link-template.php | Retrieves the permalink for a post type archive. |
| WP_Admin_Bar::initialize() wp-includes/class-wp-admin-bar.php | Initializes the admin bar. |
| wp_admin_bar_site_menu() wp-includes/admin-bar.php | Adds the “Site Name” menu. |
| wp_admin_bar_my_sites_menu() wp-includes/admin-bar.php | Adds the “My Sites/[Site Name]” menu and all submenus. |
| install_blog() wp-includes/ms-deprecated.php | Install an empty blog. |
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_home_url