0

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;

        }
    }
  • Which hook do you use? I think this is a function and the first line is missing. – moped May 22 at 13:23
  • @moped it is not based on hook. it is related to a custom page and custom form for login – Sh.Dehnavi May 22 at 13:28
  • You could use login_redirect – moped May 22 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 May 22 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 May 22 at 13:48
0

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.
}
  • thanks for answering, I found the answer myself – Sh.Dehnavi May 23 at 5:02
0
  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

  • thanks for answering, I found the answer myself – Sh.Dehnavi May 23 at 5:02
0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.