allowed_redirect_hosts
云策文档标注
概述
allowed_redirect_hosts 是一个 WordPress 过滤器,用于修改允许重定向的主机列表。它通常与 wp_validate_redirect() 函数结合使用,确保重定向 URL 的安全性。
关键要点
- 过滤器名称:allowed_redirect_hosts
- 参数:$hosts(数组,当前允许的主机名列表)和 $host(字符串,重定向目标的主机名,未设置时为空字符串)
- 用途:扩展或限制重定向操作中允许的主机,防止恶意重定向
- 相关函数:wp_validate_redirect() 用于验证重定向 URL
- 引入版本:WordPress 2.3.0
代码示例
function my_allowed_redirect_hosts( $hosts ) {
$my_hosts = array(
'blog.example.com',
'codex.example.com',
);
return array_merge( $hosts, $my_hosts );
};
add_filter( 'allowed_redirect_hosts', 'my_allowed_redirect_hosts' );
原文内容
Filters the list of allowed hosts to redirect to.
Parameters
$hostsstring[]-
An array of allowed host names.
$hoststring-
The host name of the redirect destination; empty string if not set.
Source
$allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array( $wpp['host'] ), isset( $lp['host'] ) ? $lp['host'] : '' );
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |
Skip to note 2 content
garretmh
Allow additional redirect hosts
You can allow additional hosts when validating redirects by using the allowed_redirect_hosts filter.
function my_allowed_redirect_hosts( $hosts ) { $my_hosts = array( 'blog.example.com', 'codex.example.com', ); return array_merge( $hosts, $my_hosts ); }; add_filter( 'allowed_redirect_hosts', 'my_allowed_redirect_hosts' );