get_home_url

Retrieves the URL for a given site where the front end is accessible.

Description

(string) get_home_url( (constant) $blog_id = null, (string) $path = '', (null) $scheme = null ); 

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.

Returns (string)

Home URL link with optional path appended.

Parameters (3)

0. $blog_id — Optional. (constant) => null
Site ID. Default null (current site).
1. $path — Optional. (string) => ''
Path relative to the home URL. Default empty.
2. $scheme — Optional. (null) => null
Scheme to give the home URL context. Accepts http,, https, relative, rest, or null. Default null.

Usage

  1. if ( !function_exists( 'get_home_url' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Site ID. Default null (current site). 
  5. $blog_id = null; 
  6.  
  7. // Optional. Path relative to the home URL. Default empty. 
  8. $path = ''; 
  9.  
  10. // Optional. Scheme to give the home URL context. Accepts 
  11. // 'http', 'https', 'relative', 'rest', or null. Default null. 
  12. $scheme = null; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_home_url($blog_id, $path, $scheme); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_home_url( $blog_id = null, $path = '', $scheme = null ) { 
  2. global $pagenow; 
  3.  
  4. $orig_scheme = $scheme; 
  5.  
  6. if ( empty( $blog_id ) || !is_multisite() ) { 
  7. $url = get_option( 'home' ); 
  8. } else { 
  9. switch_to_blog( $blog_id ); 
  10. $url = get_option( 'home' ); 
  11.  
  12. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) { 
  13. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) 
  14. $scheme = 'https'; 
  15. else 
  16. $scheme = parse_url( $url, PHP_URL_SCHEME ); 
  17.  
  18. $url = set_url_scheme( $url, $scheme ); 
  19.  
  20. if ( $path && is_string( $path ) ) 
  21. $url .= '/' . ltrim( $path, '/' ); 
  22.  
  23. /** 
  24. * Filters the home URL. 
  25. * @since 3.0.0 
  26. * @param string $url The complete home URL including scheme and path. 
  27. * @param string $path Path relative to the home URL. Blank string if no path is specified. 
  28. * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',  
  29. * 'relative', 'rest', or null. 
  30. * @param int|null $blog_id Site ID, or null for the current site. 
  31. */ 
  32. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );