wp_replace_insecure_home_url()
云策文档标注
概述
wp_replace_insecure_home_url() 函数用于在内容中替换站点的非安全 HTTP URL 为 HTTPS 版本,前提是配置允许。它基于 wp_should_replace_insecure_home_url() 的检查结果执行替换操作。
关键要点
- 函数替换内容中站点的 HTTP URL 为 HTTPS URL,包括转义版本。
- 使用前需通过 wp_should_replace_insecure_home_url() 判断是否应执行替换。
- 参数 $content 为必需,表示要替换 URL 的内容,返回过滤后的字符串。
- 函数从 WordPress 5.7.0 版本引入。
代码示例
function wp_replace_insecure_home_url( $content ) {
if ( ! wp_should_replace_insecure_home_url() ) {
return $content;
}
$https_url = home_url( '', 'https' );
$http_url = str_replace( 'https://', 'http://', $https_url );
// Also replace potentially escaped URL.
$escaped_https_url = str_replace( '/', '/', $https_url );
$escaped_http_url = str_replace( '/', '/', $http_url );
return str_replace(
array(
$http_url,
$escaped_http_url,
),
array(
$https_url,
$escaped_https_url,
),
$content
);
}
原文内容
Replaces insecure HTTP URLs to the site in the given content, if configured to do so.
Description
This function replaces all occurrences of the HTTP version of the site’s URL with its HTTPS counterpart, if determined via wp_should_replace_insecure_home_url().
Parameters
$contentstringrequired-
Content to replace URLs in.
Source
function wp_replace_insecure_home_url( $content ) {
if ( ! wp_should_replace_insecure_home_url() ) {
return $content;
}
$https_url = home_url( '', 'https' );
$http_url = str_replace( 'https://', 'http://', $https_url );
// Also replace potentially escaped URL.
$escaped_https_url = str_replace( '/', '/', $https_url );
$escaped_http_url = str_replace( '/', '/', $http_url );
return str_replace(
array(
$http_url,
$escaped_http_url,
),
array(
$https_url,
$escaped_https_url,
),
$content
);
}
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |