0

I want to make a multi-user website. Different types of users are Students, Teachers, Parents, Admin and counselor. I want to redirect each user to specific pages based on their type after logging in. How do I proceed? Is any plugin available?

How do I carry out registration and login?

Say, students should be redirected to a page student.php if and only if he is a student. Teachers should be redirected to say teacher.php.

All types of users have different dashboards.

  • You can save their roles in meta fields and on login you can check the field and add the redirection accordingly. – Akshat Jun 19 '18 at 12:33
1

You would make use of the login_redirect filter.

The example in the documentation looks a lot like what you are asking:

/**
 * 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 wpse306427_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {
        //check for subscribers
        if (in_array('subscriber', $user->roles)) {
            // redirect them to another URL, in this case, the homepage 
            $redirect_to =  home_url();
        }
    }

    return $redirect_to;
}

add_filter( 'login_redirect', 'wpse306427_login_redirect', 10, 3 );

You would put this code in a plug-in or in your theme's function.php.

  • This is only good for the moment of login... users can still log in and the enter a different url – Sagive SEO Jul 20 at 20:17
  • 1
    @SagiveSEO That's correct, this code does not enforce URL restrictions. There are other great answers for that on this stack exchange if you need this. – Olivier Jul 22 at 16:34

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.