set_url_scheme()WP 3.4.0

Sets the current site protocol for the specified URL (link). If relative is specified in $scheme, it will return a relative URL.

The function does NOT add a scheme to an empty URL. If you specify like: example.org/what/ever, the function will not recognize that this is a URL and will not process such a string (it will return it as is). For this reason the original URL must always have a base scheme, for example, https://.

$url = 'example.org/what/ever/';
echo set_url_scheme( $url, 'https' ); // example.org/what/ever
Used By: get_home_url()
1 time — 0.000017 sec (very fast) | 50000 times — 0.07 sec (speed of light) | PHP 7.0.2, WP 4.4.1
Hooks from the function

Returns

String. Modified URL for the selected scheme.

Usage

set_url_scheme( $url, $scheme );
$url(string) (required)
URL containing the http(s) protocol.
$scheme(string/null)

Scheme according to which the URL should be changed. May be:

http
https
relative    — returns a relative URL (without the domain).
login       — protocol of the login page
login_post
admin
rest
rpc
null        — current site protocol is_ssl()

Default: null (current site protocol is_ssl())

Examples

0

#1 Demo of protocol changes in the URL.

The code was executed on a site with HTTPS protocol by default:

$res = [
	set_url_scheme( 'http://example.com/foo' ),             //> https://example.com/foo OR http://example.com/foo
	set_url_scheme( 'http://example.com/foo', 'http' ),     //> http://example.com/foo
	set_url_scheme( 'http://example.com/foo', 'https' ),    //> https://example.com/foo
	set_url_scheme( 'http://example.com/foo', 'relative' ), //> /foo

	set_url_scheme( '//example.com/foo' ),               //> https://example.com/foo OR http://example.com/foo
	set_url_scheme( '//example.com/foo', 'http' ),       //> http://example.com/foo
	set_url_scheme( '//example.com/foo', 'https' ),      //> https://example.com/foo
	set_url_scheme( '//example.com/foo', 'relative' ),   //> /foo

	// Doesn't work with non-URL strings (return it as is)
	set_url_scheme( '/foo', 'https' ),             //> /foo
	set_url_scheme( 'example.com/foo', 'https' ),  //> example.com/foo

	// non-standard
	set_url_scheme( '///example.org/foo', 'https' ), //> https:///example.org/foo
];

print_r( $res );

Changelog

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

set_url_scheme() code WP 6.8.3

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 );
}