set_url_scheme() WordPress Function
The set_url_scheme() function allows you to change the URL scheme for a given URL. This is useful if you need to change the scheme for a specific URL, such as when changing from HTTP to HTTPS.
set_url_scheme( string $url, string|null $scheme = null ) #
Sets the scheme for a URL.
Parameters
- $url
(string)(Required)Absolute URL that includes a scheme
- $scheme
(string|null)(Optional) Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
Default value: null
Return
(string) URL with chosen scheme.
Source
File: wp-includes/link-template.php
3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 | 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 ( substr ( $url , 0, 2 ) === '//' ) { $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 ); } |
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.4.0 | The 'rest' scheme was added. |
3.4.0 | Introduced. |