0

I would like to redirect my Subscribers to our Member Resources page when they login, but only when they are logging in from a Page ( is_page() ). When they log in from one of our custom post types I would like them to stay on this post. We we using a pop-up modal for login, so they are never redirected anywhere during the login process.

I would like Admins, Editors & Authors to be redirected to the WordPress admin dashboard when they log in.

This is what I have tried, but it's not working. Nothing happens when I log in as an Admin or a Subscriber - I remain on the page I'm on:

function custom_login_redirect( $redirect_to, $request, $user ) {
  if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    if ( in_array( 'administrator', $user->roles ) || in_array( 'editor', $user->roles ) || in_array( 'author', $user->roles ) ) {
      $redirect_to = admin_url();
    }
  } elseif ( isset( $user->roles ) && is_array( $user->roles ) ) {
    if ( in_array( 'subscriber', $user->roles ) && is_page() ) {
      $redirect_to = home_url('member-resources');
    }
  } else {
    return $redirect_to;
  }
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );

1 Answer 1

0

Your logic is not quite correct as in some cases you will never hit the one return statement. Try this instead:

function custom_login_redirect( $redirect_to, $request, $user ) {
  if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    if ( in_array( 'administrator', $user->roles ) || in_array( 'editor', $user->roles ) || in_array( 'author', $user->roles ) ) {
      return $redirect_to;
    } else {
      if ( in_array( 'subscriber', $user->roles ) && is_page() ) {
        return home_url('/member-resources/');
    } 
  } else {
    return $redirect_to;
  }
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );
2
  • Thanks @ScottM! It's still not working so I think it must be related to how the login modal is set up. I'm guessing it would work if we were logging in from /wp-login.php
    – megseoh
    Jan 4 at 17:13
  • @megseoh - are you using a plugin for the modal login dialog? If so, you will need to follow up with the plugin developer to see how to get this working.
    – ScottM
    Jan 4 at 21:19

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.