wp_redirect()
wp_redirect( string $location, int $status = 302, strin…
wp_redirect( string $location, int $status = 302, string $x_redirect_by = ‘WordPress’ )
重定向到另一页。
Redirects to another page.
重定向到另一页。
Redirects to another page.
目录锚点:#说明#参数#源码#笔记
说明(Description)
注意:wp_redirect()不会自动退出,并且几乎总是在后面调用exit;:wp_redirect($url);exit;退出也可以通过使用wp_redirect()作为条件并结合“wp_redirect”和“wp_redirect_location”筛选器:if(wp_redirect($url)){exit;}
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $location | (string) | 要重定向到的路径或URL。 |
| $status | (int) | 要使用的HTTP响应状态代码。默认值为“302”(暂时移动)。 |
| $x_redirect_by | (string) | 执行重定向的应用程序。 |
源码(Source)
/**
* Redirects to another page.
*
* @since 1.5.1
*
* @global bool $is_IIS
*
* @param string $location The path to redirect to.
* @param int $status Status code to use.
* @return bool False if $location is not provided, true otherwise.
*/
function wp_redirect($location, $status = 302) {
global $is_IIS;
/**
* Filter the redirect location.
*
* @since 2.1.0
*
* @param string $location The path to redirect to.
* @param int $status Status code to use.
*/
$location = apply_filters( 'wp_redirect', $location, $status );
/**
* Filter the redirect status code.
*
* @since 2.3.0
*
* @param int $status Status code to use.
* @param string $location The path to redirect to.
*/
$status = apply_filters( 'wp_redirect_status', $status, $location );
if ( ! $location )
return false;
$location = wp_sanitize_redirect($location);
if ( !$is_IIS && PHP_SAPI != 'cgi-fcgi' )
status_header($status); // This causes problems on IIS and some FastCGI setups
header("Location: $location", true, $status);
return true;
}
endif;
if ( !function_exists('wp_sanitize_redirect') ) :| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 5.4.0 | wp-includes/pluggable.php | 9 | 5 |
笔记(Notes)
wp_redirect()不验证$location是否是对当前主机的引用。这意味着,如果向该函数传递用户提供的$location,则该函数容易受到打开重定向的攻击。因此,最佳做法是始终使用wp_safe_redirect(),因为它将使用wp_validate_redirect()来确保$location指向当前主机。仅当您特别尝试重定向到另一个站点时才使用wp_redirect(),然后可以硬编码URL。
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

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