set_url_scheme( string $url, string|null $scheme = null ): string

Sets the scheme for a URL.

Parameters

$urlstringrequired
Absolute URL that includes a scheme
$schemestring|nulloptional
Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.

Default:null

Return

string URL with chosen scheme.

Source

function set_url_scheme( $url, $scheme = null ) {
	$orig_scheme = $scheme;

	if ( ! $scheme ) {
		$scheme = is_ssl() ? 'https' : 'http';
	} elseif ( 'admin' === $scheme || 'login' === $scheme || 'login_post' === $scheme || 'rpc' === $scheme ) {
		$scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
	} elseif ( 'http' !== $scheme && 'https' !== $scheme && 'relative' !== $scheme ) {
		$scheme = is_ssl() ? 'https' : 'http';
	}

	$url = trim( $url );
	if ( str_starts_with( $url, '//' ) ) {
		$url = 'http:' . $url;
	}

	if ( 'relative' === $scheme ) {
		$url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
		if ( '' !== $url && '/' === $url[0] ) {
			$url = '/' . ltrim( $url, "/ \t\n\r\0\x0B" );
		}
	} else {
		$url = preg_replace( '#^\w+://#', $scheme . '://', $url );
	}

	/**
	 * Filters the resulting URL after setting the scheme.
	 *
	 * @since 3.4.0
	 *
	 * @param string      $url         The complete URL including scheme and path.
	 * @param string      $scheme      Scheme applied to the URL. One of 'http', 'https', or 'relative'.
	 * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
	 *                                 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
	 */
	return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
}

Hooks

apply_filters( ‘set_url_scheme’, string $url, string $scheme, string|null $orig_scheme )

Filters the resulting URL after setting the scheme.

Changelog

VersionDescription
4.4.0The 'rest' scheme was added.
3.4.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Important Note: set_url_scheme() does NOT add a scheme to a bare URL. If you pass in ‘example.org/what/ever’, you’ll get ‘example.org/what/ever’ out the other side. For this reason, you should always add a basic scheme to URLs if you know the input URL won’t have one, e.g. ‘https://’.

    $url = 'example.org/what/ever/'
    print_r( set_url_scheme( $url, 'https' ) );
    // Result: 'example.org/what/ever'
    
    print_r( set_url_scheme( 'http://' . $url, 'https' ) );
    // Result: 'https://example.org/what/ever ('https' if is_ssl() is true, otherwise 'http')
  2. Skip to note 4 content

    Usage with is_ssl()

    One of the nice things about set_url_scheme() is that if you’re in an SSL environment and everything is working properly, you don’t necessarily need to define a scheme, as set_url_scheme() will do that for you.

    For example:

    $url = 'http://example.org/some/permalink';
    
    print_r( set_url_scheme( $url ) );
    // If is_ssl() is true:
    // Result: 'https://example.org/some/permalink
    //
    // If is_ssl() is false:
    // Result: 'http://example.org/some/permalink (no change)

You must log in before being able to contribute a note or feedback.