所需内容:如果GET var“login”设置为“incorrect”或“empty”,请将用户重定向到主页。否则将他重定向到任何推荐人。
但没有像这样的钩子wp-login
. 但是有一个有用的钩子叫做login_redirect
.
还有一个问题:The PHP manual says about $_SERVER[\'HTTP_REFERER\']
:
将用户代理引用到当前页面的页面(如果有)的地址。这是由用户代理设置的。并非所有的用户代理都会设置此功能,有些代理提供了将HTTP\\U REFERER作为功能进行修改的功能。简言之,它真的不可信。
add_action( \'login_redirect\', \'redirect_on_login\' );
function redirect_on_login( $redirect_to ) {
$homepage = get_site_url();
// get whatever is stored in the GET-var \'login\'
$login = filter_input( INPUT_GET, \'login\', FILTER_SANITIZE_STRIPPED );
// if the content of \'login\' is in the group (here as array), redirect to the homepage
if ( in_array( $login, array( \'incorrect\', \'empty\' ) ) ) {
return $homepage;
} else {
// $_SERVER[\'HTTP_REFERER\'] cannot really be trusted and $referrer can be empty.
// setup a default location for redirecting.
// @see: http://php.net/manual/en/reserved.variables.server.php
$referrer = ( isset( $_SERVER[\'HTTP_REFERER\'] ) && ! empty( $_SERVER[\'HTTP_REFERER\'] ) ) ?
$_SERVER[\'HTTP_REFERER\'] : $homepage;
// wp-login.php use wp_safe_redirect, this means only pages on the same domain are accepted
// @see: http://codex.wordpress.org/Function_Reference/wp_safe_redirect
// get the host of our blog (strip scheme like http:// and https://)
$host = parse_url( $homepage, PHP_URL_HOST );
// check if the referrer is on the same host as the blog
$redirect_to = ( false != stristr( $referrer, $host ) ) ?
$referrer : $homepage;
return $redirect_to;
}
// if everything fails, return the original value
return $redirect_to;
}
我们将在这个脚本中结束。首先使用右钩子(
login_redirect
). 这是一个筛选器,它接受一个值,即重定义目标。因此,我们的函数应该返回一个新的或修改过的重定向目标。
在我们的函数中,获取get varlogin
并检查是否将其设置为可接受的值之一(“不正确”或“空”)。如果是这样,请将重定向目标设置为主页(站点url)。
如果不是,请尝试获取$_SERVER[\'HTTP_REFERER\']
. 如上所述,我们无法信任此值,因此如果$_SERVER[\'HTTP_REFERER\']
是空的或我们不能使用的东西。
最后检查重定向目标(可能是referer)是否与博客位于同一主机上。这是必要的,因为login_redirect
使用wp_safe_redirect()
此函数只接受本地目标作为重定向。