set_url_scheme

Sets the scheme for a URL.

Description

(string) set_url_scheme( (string) $url, (null) $scheme = null ); 

Returns (string)

$url URL with chosen scheme.

Parameters (2)

0. $url (string)
Absolute URL that includes a scheme
1. $scheme — Optional. (null) => null
Scheme to give $url. Currently http,, https, login, login_post, admin, relative, rest, rpc, or null. Default null.

Usage

  1. if ( !function_exists( 'set_url_scheme' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Absolute URL that includes a scheme 
  5. $url = ''; 
  6.  
  7. // Optional. Scheme to give $url. Currently 'http', 'https', 'login', 
  8. // 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null. 
  9. $scheme = null; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = set_url_scheme($url, $scheme); 
  13.  

Defined (1)

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

/wp-includes/link-template.php  
  1. function set_url_scheme( $url, $scheme = null ) { 
  2. $orig_scheme = $scheme; 
  3.  
  4. if ( ! $scheme ) { 
  5. $scheme = is_ssl() ? 'https' : 'http'; 
  6. } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) { 
  7. $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http'; 
  8. } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) { 
  9. $scheme = is_ssl() ? 'https' : 'http'; 
  10.  
  11. $url = trim( $url ); 
  12. if ( substr( $url, 0, 2 ) === '//' ) 
  13. $url = 'http:' . $url; 
  14.  
  15. if ( 'relative' == $scheme ) { 
  16. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) ); 
  17. if ( $url !== '' && $url[0] === '/' ) 
  18. $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" ); 
  19. } else { 
  20. $url = preg_replace( '#^\w+://#', $scheme . '://', $url ); 
  21.  
  22. /** 
  23. * Filters the resulting URL after setting the scheme. 
  24. * @since 3.4.0 
  25. * @param string $url The complete URL including scheme and path. 
  26. * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'. 
  27. * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',  
  28. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. 
  29. */ 
  30. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );