Site icon Hip-Hop Website Design and Development

How to redirect Subscribers on login to specific page, when logging in from a Page

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 );