如何在Wordpress中登录后将具有特定“ROLE”的用户重定向到特定页面

时间:2021-11-19 02:51:06

I have created a new user role named student_role I want to redirect the user with this role to form page(which I created from wp front end) when he logs in.

我创建了一个名为student_role的新用户角色我希望在登录时将具有此角色的用户重定向到表单页面(我是从wp前端创建的)。

I tried peter login redirect plugin but failed.

我尝试过peter login redirect插件但是失败了。

1 个解决方案

#1


Try this :

试试这个 :

function rohil_login_redirect_based_on_roles($user_login, $user) {

    if( in_array( 'student_role',$user->roles ) ){
        exit( wp_redirect('Your page link goes here' ) );
    }   
}

add_action( 'wp_login', 'rohil_login_redirect_based_on_roles', 10, 2);

Explanation

If you look at the codex, you will find that wp_login provides two parameter: 1) $user_login which will return a string and 2) $user will return an object containing all the details.

如果你看一下codex,你会发现wp_login提供了两个参数:1)$ user_login将返回一个字符串,2)$ user将返回一个包含所有细节的对象。

But you need to make sure that you also pass priority to that parameters otherwise It will give you an error.

但是你需要确保你也将优先级传递给那些参数,否则它会给你一个错误。

If you want to see what data are into that parameter, you can run below code for learning purpose only.

如果要查看该参数中的数据,可以运行以下代码仅用于学习目的。

function rohil_login_redirect_based_on_roles($user_login, $user) {

    var_dump($user); // Will give you an object
    var_dump($user_login); //Will give you a string
    die();

}
add_action( 'wp_login', 'rohil_login_redirect_based_on_roles', 10, 2);

Make sure you delete above code from functions.php after checking what data it contains

确认在检查了包含哪些数据后,从functions.php中删除了上面的代码

So as you can $user object contains roles which shows current logged in user's role. So I simple checked that if($user->roles[0] === 'student_role') if current logged in user is having student_role then wp_redirect('Your page link goes here') redirect them to some page.

因此,$ user对象包含显示当前登录用户角色的角色。所以我简单地检查了if($ user-> roles [0] ==='student_role')如果当前登录的用户有student_role然后wp_redirect('你的页面链接在这里')将它们重定向到某个页面。

Let me know if you have any doubt.

如果您有任何疑问,请告诉我。

#1


Try this :

试试这个 :

function rohil_login_redirect_based_on_roles($user_login, $user) {

    if( in_array( 'student_role',$user->roles ) ){
        exit( wp_redirect('Your page link goes here' ) );
    }   
}

add_action( 'wp_login', 'rohil_login_redirect_based_on_roles', 10, 2);

Explanation

If you look at the codex, you will find that wp_login provides two parameter: 1) $user_login which will return a string and 2) $user will return an object containing all the details.

如果你看一下codex,你会发现wp_login提供了两个参数:1)$ user_login将返回一个字符串,2)$ user将返回一个包含所有细节的对象。

But you need to make sure that you also pass priority to that parameters otherwise It will give you an error.

但是你需要确保你也将优先级传递给那些参数,否则它会给你一个错误。

If you want to see what data are into that parameter, you can run below code for learning purpose only.

如果要查看该参数中的数据,可以运行以下代码仅用于学习目的。

function rohil_login_redirect_based_on_roles($user_login, $user) {

    var_dump($user); // Will give you an object
    var_dump($user_login); //Will give you a string
    die();

}
add_action( 'wp_login', 'rohil_login_redirect_based_on_roles', 10, 2);

Make sure you delete above code from functions.php after checking what data it contains

确认在检查了包含哪些数据后,从functions.php中删除了上面的代码

So as you can $user object contains roles which shows current logged in user's role. So I simple checked that if($user->roles[0] === 'student_role') if current logged in user is having student_role then wp_redirect('Your page link goes here') redirect them to some page.

因此,$ user对象包含显示当前登录用户角色的角色。所以我简单地检查了if($ user-> roles [0] ==='student_role')如果当前登录的用户有student_role然后wp_redirect('你的页面链接在这里')将它们重定向到某个页面。

Let me know if you have any doubt.

如果您有任何疑问,请告诉我。