简体   繁体   English

将登录的Wordpress用户重定向到登录屏幕之外,并根据其自定义元数据返回特定的URL

[英]Redirecting a logged-in wordpress user away from the sign-in screen and back to a specific url based on their custom metadata

I currently have a solution which: a) requires login before you can access the site b) redirects to login page (note this is also the home page/front page) if the person attempts to hit any page in the site but is not logged in c) directs the user on successful login to a site specific url most suitable to them based on some custom meta data in their profile 我目前有一个解决方案:a)如果需要访问网站上的任何页面但未登录,则必须先登录才能访问该网站b)重定向到登录页面(请注意,这也是主页/首页)在c)中,根据用户个人资料中的一些自定义元数据,将用户成功登录后引导至最适合他们的特定于站点的URL

However I want to set up a further redirect should a user who is already logged-in hit the login page again and effectively achieve exactly the same re-directions as in C above. 但是,如果已经登录的用户再次点击登录页面并有效实现与上述C完全相同的重定向,我想设置进一步的重定向。

I've attempted to utilise existing add_login_check code which uses is_user_logged_in and wp_redirect ... but I can't seem to attach additional if and elseif statements. 我试图利用现有的使用is_user_logged_inwp_redirect add_login_check代码...但是我似乎无法附加其他if和elseif语句。 So something like this works great for a single redirect but I need to enhance it to cope with more conditions 所以这样的事情对于单个重定向非常有用,但是我需要对其进行增强以应对更多情况

add_action('wp', 'add_login_check');

function add_login_check()
{
if ( is_user_logged_in() && is_page(86) ) {
    wp_redirect('/desired_url');
    exit;
}
}
add_action('wp', 'add_login_check');

So I attempted to modify to: 所以我尝试修改为:

function add_login_check()
{
if ( is_user_logged_in() && is_page(86) && 'CustomResponse1' == 
get_user_meta( $user->ID, 'Custom_Group1', true ) && 'CustomResponse2' == 
get_user_meta( $user->ID, 'CustomGroup2', true )) {
    wp_redirect('/desired_url');
    exit;
}
}
add_action('wp', 'add_login_check');

I've added && 'CustomResponse1' == get_user_meta( $user->ID, 'Custom_Group1', true ) && 'CustomResponse2' == get_user_meta( $user->ID, 'CustomGroup2', true ) to that standard code, but when a logged-in user returns to the login page they just stay there and it does not redirect. 我已在该标准代码中添加&& 'CustomResponse1' == get_user_meta( $user->ID, 'Custom_Group1', true ) && 'CustomResponse2' == get_user_meta( $user->ID, 'CustomGroup2', true )当登录用户返回登录页面时,他们只会停留在该页面,并且不会重定向。

Any help on this would be greatly appreciated. 任何帮助,将不胜感激。 In the above my users have two additional fields (I've referred to CustomGroups 1 & 2). 在上面,我的用户有两个附加字段(我已经提到了CustomGroups 1和2)。 And then there are selections which can be made in those fields (I've referred to CustomResponses 1 & 2) - based on these alongside the request page and current status being logged in I want them to be redirected. 然后,可以在那些字段中进行选择(我已经提到了CustomResponses 1和2)-基于这些以及请求页面和正在登录的当前状态,我希望它们被重定向。

I want to build in some additonal elseif statements so the redirects are dependent on different customresponses. 我想构建一些附加的elseif语句,以便重定向依赖于不同的customresponse。

Any help greatly appreciated. 任何帮助,不胜感激。

You are trying to use a undefined variable $user . 您正在尝试使用未定义的变量$user Use wp_get_current_user() function to retrieve the current user object. 使用wp_get_current_user()函数检索当前用户对象。

function add_login_check()
{
    if ( is_user_logged_in() )
    { 
        $user = wp_get_current_user();
        if ( is_page(86) && 
             'CustomResponse1' == get_user_meta( $user->ID, 'Custom_Group1', true ) && 
             'CustomResponse2' == get_user_meta( $user->ID, 'CustomGroup2', true )) 
        {
            wp_redirect('/desired_url');
            exit;
        }
    }
}

This was the solution used in the end 这是最终使用的解决方案

function redirect_my_page()
{
global $current_user;
$user = wp_get_current_user();
if ( is_user_logged_in() && is_page(86) && 'CustomResponse1' == get_user_meta( $user- 
>ID, 'CustomGroup1', true ) && 'CustomResponse2' == get_user_meta( $user->ID, 
'CustomGroup2', true ))
{
wp_redirect('http://desiredurl/');
exit;
}
elseif ( is_user_logged_in() && is_page(86) && 'CustomResponse1a' == get_user_meta( 
$user->ID, 'CustomResponse1', true ) && 'CustomResponse2a' == get_user_meta( $user- 
>ID, 'CustomResponse2', true ))
{
wp_redirect('http://desiredurl/');
exit;
}
}
add_action('template_redirect', 'redirect_my_page');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM