Redirect after login based on user role (custom login page)

I created a custom login page and everything is OK but there is an issue. I want to redirect users to different pages ba

I created a custom login page and everything is OK but there is an issue. I want to redirect users to different pages based on their roles after login. My code is below but I do not know why it is not working:

        if (!$has_error) {
            $info = array();
            $info['user_login'] = $user_login;
            $info['user_password'] = $password;
            $info['emember'] = $remember;
            $user_signon = wp_signon($info,false);

        if(is_wp_error($user_signon)){
            $has_error = true;
            $message[] = "an error";
        }else{

            $currentuser = wp_get_current_user();
            $currentuserroles = $currentuser -> roles;
            if( in_array( 'author', $currentuserroles ) ){
                $redirect = site_url().'/dashboard-teacher';

            }elseif( in_array( 'subscriber', $currentuserroles ) ){
                $redirect = site_url().'/dashboardwwp-student';

            }else{
                $redirect = site_url();

            }
            wp_redirect( $redirect );
            exit;

        }
    }

I created a custom login page and everything is OK but there is an issue. I want to redirect users to different pages based on their roles after login. My code is below but I do not know why it is not working:

        if (!$has_error) {
            $info = array();
            $info['user_login'] = $user_login;
            $info['user_password'] = $password;
            $info['emember'] = $remember;
            $user_signon = wp_signon($info,false);

        if(is_wp_error($user_signon)){
            $has_error = true;
            $message[] = "an error";
        }else{

            $currentuser = wp_get_current_user();
            $currentuserroles = $currentuser -> roles;
            if( in_array( 'author', $currentuserroles ) ){
                $redirect = site_url().'/dashboard-teacher';

            }elseif( in_array( 'subscriber', $currentuserroles ) ){
                $redirect = site_url().'/dashboardwwp-student';

            }else{
                $redirect = site_url();

            }
            wp_redirect( $redirect );
            exit;

        }
    }
Share Improve this question edited May 22, 2019 at 14:03 Sh.Dehnavi asked May 22, 2019 at 13:07 Sh.DehnaviSh.Dehnavi 1093 silver badges18 bronze badges 8
  • Which hook do you use? I think this is a function and the first line is missing. – moped Commented May 22, 2019 at 13:23
  • @moped it is not based on hook. it is related to a custom page and custom form for login – Sh.Dehnavi Commented May 22, 2019 at 13:28
  • You could use login_redirect – moped Commented May 22, 2019 at 13:33
  • 1 We can better help if we can fully understand the issue. When you say, "I do not know why it is not working" it would be helpful for us to know what IS happening (the "actual" behavior). – Ted Stresen-Reuter Commented May 22, 2019 at 13:36
  • 1 That's helpful. Thanks for the clarification. And now some follow-up questions. What roles are defined (can you kindly do a print_r($currentuserroles)?)? Are you certain /dashboard-teacher and the other page/post actually exist? What happens when you visit either of these pages as an author or subscriber? What is the HTTP response code (how certain are you that you aren't being redirected twice: once to your page and then to the home page)? – Ted Stresen-Reuter Commented May 22, 2019 at 13:48
 |  Show 3 more comments

3 Answers 3

Reset to default 3

Try this. It works for me.

add_filter( 'login_redirect', 'redirect_non_admin_to_dashboard'), 10, 3 );
function redirect_non_admin_to_dashboard($redirect_to, $requested_redirect_to, $user ) {
    global $user;
    if( ! isset( $user->ID ) ) {
        return $redirect_to;
    }

    if ( ! in_array( 'author', (array) $user->roles ) ) {
        $redirect_to  =  site_url().'/dashboard-teacher';
    }elseif( in_array( 'subscriber',(array) $user->roles ) ){
            $redirect_to = site_url().'/dashboardwwp-student';

        }else{
            $redirect_to = site_url();

        }
    return wp_validate_redirect( $redirect_to, home_url() ); // return a safe redirect url between the site.
}

i found the answer myself. i used wp_get_current_user() while wp_get_current_user() may not be set at this step then i tried the user_can with $user_signon that i defined before for user login. so the new codes:

        if( user_can($user_signon, 'author') ){

            $redirect = site_url().'/pishkhan-teacher';

        }elseif( user_can($user_signon, 'subscriber') ){

            $redirect = site_url('/pishkhan-student');

        }elseif( user_can($user_signon, 'wpas_user') ){

            $redirect = site_url('/pishkhan-student');

        }else{

            $redirect = site_url();

        }
        wp_redirect( $redirect );
        exit;
  1. You have a typo in your code: $info['emember'] = $remember; should be $info['remember'] = $remember; ("remember" is misspelled) but I don't think this is the source of the problem.
  2. wp_get_current_user(); returns either a WP_Error object or a WP_User object (the former if an error, the latter if an authenticated user exists). Rather than calling wp_get_current_user(), just use the WP_User object that was returned: $user_signon->roles. The less code, the less chance for error (although I don't think this is the error either).
  3. site_url() is technically a template tag. Although it seems to return the site URL, it might not work if it is not "in the loop". Instead of using site_url, try swapping it out for get_site_url(). Also, both site_url() and get_site_url() accept two, optional parameters: 1) a path to append to the site URL and 2) the scheme to use. Instead of appending the path with the string concatenation operator (.), just put the path as the first parameter of the function: $redirect = get_site_url('/dashboard-teacher'). And again, I don't think either of these are the problem you are having.
  4. Redirection can be tricky in general, especially with WordPress. If any amount of data is output to the browser prior to calling redirect (e.g.: you use wp_redirect() in a template rather than in a hook called in functions.php), then redirection is not guaranteed even though these days both WordPress and browsers have some flexibility and can often handle this issue correctly.

Other than the items I've mentioned above, your code looks fine and should work. So long as you are executing the login code before the template_redirect hook, it should work. Once you've addressed the issues above, if it continues to fail, then I would need to see the rest of the code you are using to process the form (what hook you are using) and probably I would need to see all the info regarding permissions and to verify the destination URLs before I could make any recommendations.

HTH

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745470989a4629130.html

相关推荐

  • Redirect after login based on user role (custom login page)

    I created a custom login page and everything is OK but there is an issue. I want to redirect users to different pages ba

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信