set_url_scheme

函数
set_url_scheme ( $url, $scheme = null )
参数
  • (string) $url Absolute URL that includes a scheme
    Required:
  • (string|null) $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
    Required:
    Default: null
返回值
  • (string) URL with chosen scheme.
定义位置
相关方法
set_current_screenget_current_themeget_current_screenget_usermetaget_theme
引入
3.4.0
弃用
-

set_url_scheme: 这是一个WordPress的函数,为一个给定的URL设置URL方案。它通常用于将URL方案从http改为https或反之亦然: 这个函数需要两个参数:要修改的URL和新的URL方案。

设置一个URL的方案。

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, "/ tnrx0B" );
		}
	} 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 );
}

常见问题

FAQs
查看更多 >