1

I'm trying to accomplish this simple task through different plugins, but I can't find the right one for different reasons.

I have a Login Page with a login form. When user logs in, he's redirected to a specific page (based on user role). If the user visits again the Login Page, the login form displays a Welcome message with dashboard, profile and logout links (see pics).

enter image description here

Since I need to avoid that and need logged in user to only visit the specific page based on their role, I'd like to redirect away logged in users from Login Page to Specific Page.

eg: Paul is a user with "apple" role. He logs in and gets redirected to Apple Page. Then he browses the website and try to visit the Login Page. As he tries, he's redirected to Apple Page. The same applies to Ron, whose role is "banana", so when he tries to visit Login Page he's redirected to Banana Page.

My biggest issue is that every plugin helps with redirection AFTER login, and that is easly manageable. Do you know any easy way to accomplish that or any suggestion on where to look?

2 Answers 2

1

It kind of depends on where and how the user logs in, and what these pages are in context. The other answer is fine if logins are on the wp-login.php. You did not specify, so this answer will approach it from the standpoint of if the login is a front end page.

You will need to hook to an action, but the appropriate action depends on the context. If you need to know the page being accessed (meaning a front end page), then use template_redirect as the page will be available to is_page() (which may not be if using an earlier action such as init).

You also need to properly check if the user has a specified role (although my opinion is that you're improperly using WP roles in this kind of instance - but that's a long discussion for another time). Simply put, please DO NOT do this as current_user_can( 'apple' );. And keep in mind that in WP a user can have multiple roles assigned, so you can't just if ( 'role' == $user_role ) {....

So here's a general example:

add_action( 'template_redirect', 'my_logged_in_redirect' );
function my_logged_in_redirect() {

     // If it's the login page AND the user is logged in.
     if ( is_page( 'login' ) && is_user_logged_in() ) {

          // Get the user info.
          $user_id = get_current_user_id();
          $user = get_userdata( $user_id );

          // Determine the redirect URL
          $url = false;
          if ( in_array( 'apple', $user->roles ) ) {
               $url = '/apple/page/';
          }
          if ( in_array( 'banana', $user->roles ) ) {
               $url = '/banana/page/';
          }

          // Redirect if a $url is set
          if ( $url ) {
               wp_safe_redirect( $url );
               exit();
          }
     }
}
0

You can use admin_init to get this done. This hook fires when an admin page is starting to load and you can use it to redirect the user to a different page. Like this:

add_action('admin_init','wpse362882_redirect_login_page');
function wpse362882_redirect_login_page () {
  global $pagenow;
  if ( ($pagenow == 'wp-login.php') && is_user_logged_in() ) {
    .. do some stuff to determine $url to redirect user based on role ..
    wp_redirect($url);
    exit;
    }
  }

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.