作为一个例外,如果用户到达需要登录的点,我希望重定向到登录屏幕,并且一旦他登录并且证书条件已满(设置为myappname_allow_redirect
) 我想重定向回用户所在的最后一个url
在一些要求用户登录的短代码中:
try{
$mus = new myappnameUserService();
}catch (myappnameNeedsLoginException $e){
global $wp;
//this is the easiest way supposedly to get the current URL see here https://wordpress.stackexchange.com/questions/274569/how-to-get-url-of-current-page-displayed
$thisUrl = home_url($wp->request);
$thisUrl = urlencode($thisUrl);
myappname_redirect_to_url(\'/wp-login.php?redirect_to=\' . $thisUrl . \'&myappname_allow_redirect=1\');
}
功能。php
function myappname_login_redirect($redirect_to, $request, $user)
{
if ($user instanceof WP_User) {
if (isset($user->roles) && is_array($user->roles)) {
//check for admins
if (in_array(\'administrator\', $user->roles)) {
// redirect them to the default place
return $redirect_to;
} else {
//if something is set her for exceptions, we allow regular redirect as well
if($_GET[\'myappname_allow_redirect\']){
return $redirect_to;
}else{
//but normally we redirect to a standard url
return \'somethingelse\';
}
}
} else {
return $redirect_to;
}
}
//is there a user to check?
}
add_filter(\'login_redirect\', \'myappname_login_redirect\', 10, 3);
首先,它似乎起作用了,我被重定向到mydomain。com/wp登录。php?将\\u重定向到=xxxx(&U);myappname\\u allow\\u redirect=1
但是
myappname_login_redirect($redirect_to, $request, $user)
$redirect_to
是空的,也是空的
$request
最合适的回答,由SO网友:Toskan 整理而成
问题是。。。
myappname_login_redirect
需要返回$redirect_to
无论如何
否则重定向将丢失
背景:
wp登录。php将把redirect\\u to值呈现到一个隐藏在登录表单中的输入中。
所以到达
wp登录。php?将\\u重定向到=www.example。com公司
将呈现
一
<input type="hidden" value="www.example.com">
如果过滤器默认不返回该值,则该值将为空
所以修复。。。
function myappname_login_redirect($redirect_to, $request, $user)
{
if ($user instanceof WP_User) {
if (isset($user->roles) && is_array($user->roles)) {
//check for admins
if (in_array(\'administrator\', $user->roles)) {
// redirect them to the default place
return $redirect_to;
} else {
//if something is set her for exceptions, we allow regular redirect as well
if($_GET[\'myappname_allow_redirect\']){
return $redirect_to;
}else{
//but normally we redirect to a standard url
return \'somethingelse\';
}
}
} else {
return $redirect_to;
}
}
//is there a user to check?
return $redirect_to;
}
add_filter(\'login_redirect\', \'myappname_login_redirect\', 10, 3);