参数
$urlstring必填-
包含scheme的绝对URL
$schemestring|null可选-
给出$url的scheme。当前为
'http'、'https'、'login'、'login_post'、'admin'、'relative'、'rest'、'rpc'或null。默认:
null
返回
string 具有所选scheme的URL。
钩子
源码
更新日志
| 版本 | 描述 |
|---|---|
| 4.4.0 | 添加了'rest'方案。 |
| 3.4.0 | 开始引入 |
使用示例
重要提示:
set_url_scheme()不会向空URL添加scheme。如果你传入‘example.org/what/ever’,你会从另一边得到‘example.org/what/ever’。因此,如果您知道输入URL没有基础scheme,那么您应该始终向URl添加基础scheme,例如‘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')使用
is_ssl()set_url_scheme()的一个优点是,如果您处于SSL环境中,并且一切都正常工作,那么您不必定义scheme,因为set_url_scheme()将为您实现这一点。例如:
$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)