0

I have insert the login form on the header part and when a user is logedin i need to redirect them based on their role to their profile page(like if they are normal than normal-member.php and if pro user than pro-user.php). How can i do that using the wp_login_form.Any suggestion please...

1 Answer 1

1

You can use the login_redirect filter for this purpose:

/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $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;
        //here make if statements for your specific roles and locations
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
3
  • Will it not redirect all logged in users, regardless of from which login form they use. I think defining redirect in login form would be better.
    – Robert hue
    Sep 5, 2015 at 18:42
  • You could always check for your own login form by passing in a custom POST variable for example? Sep 6, 2015 at 18:55
  • @JukkaRautanen it works with little modification for more users
    – clap
    Sep 8, 2015 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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