wp_safe_redirect()
wp_safe_redirect( string $location, int $status = 302, …
wp_safe_redirect( string $location, int $status = 302, string $x_redirect_by = ‘WordPress’ )
使用wp_redirect()执行安全(本地)重定向。
Performs a safe (local) redirect, using wp_redirect().
目录锚点:#说明#参数#源码#笔记
说明(Description)
检查$location是否使用允许的主机(如果它有绝对路径)。因此,插件可以设置或删除列表中允许的主机。如果不允许主机,则重定向默认为siteurl上的wp admin。这可以防止恶意重定向重定向到另一个主机,但只在少数地方使用。注意:wp_safe_redirect()不会自动退出,并且应该几乎总是在调用exit之后;:wp_safe_redirect($url);exit;退出也可以通过使用wp_safe_redirect()作为条件与“wp_redirect”和“wp_redirect_location”筛选器一起使用作为条件的退出操作:if(wp_safe_redirect($url)){退出;}
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $location | (string) | 要重定向到的路径或URL。 |
| $status | (int) | 要使用的HTTP响应状态代码。默认值为“302”(暂时移动)。 |
| $x_redirect_by | (string) | 执行重定向的应用程序。 |
源码(Source)
/**
* Performs a safe (local) redirect, using wp_redirect().
*
* Checks whether the $location is using an allowed host, if it has an absolute
* path. A plugin can therefore set or remove allowed host(s) to or from the
* list.
*
* If the host is not allowed, then the redirect defaults to wp-admin on the siteurl
* instead. This prevents malicious redirects which redirect to another host,
* but only used in a few places.
*
* @since 2.3.0
*/
function wp_safe_redirect($location, $status = 302) {
// Need to look at the URL the way it will end up in wp_redirect()
$location = wp_sanitize_redirect($location);
/**
* Filter the redirect fallback URL for when the provided redirect is not safe (local).
*
* @since 4.3.0
*
* @param string $fallback_url The fallback URL to use by default.
* @param int $status The redirect status.
*/
$location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) );
wp_redirect($location, $status);
}
endif;
if ( !function_exists('wp_validate_redirect') ) :| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 5.1.0 | wp-includes/pluggable.php | 7 | 12 |
笔记(Notes)
与wp_redirect一样,除非对其进行修补以在将来以本机方式执行此操作,否则请确保包含nocache_headers();在wp_safe_重定向之前,如果您想确保访问者的浏览器不会缓存重定向页面结果(甚至在设置为使用302重定向时也可能发生),这可能会导致重定向发生的时间超过预期。
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!