0

I'm trying to redirect logged in users to a specific page based on what is in the login url.

For instance, I have added an action parameter to the login url like this: https://mywebsite.com/wp-login.php?action=ppe

I understand and know how to redirect users based on user roles, but how can I redirect based on user role AND the action parameter?

This works:

if (isset($user->roles) && is_array($user->roles) ) {
  if (in_array('member', $user->roles) ) {
    return "www.randomURL.com";
  }
}

But this does not (I log in, but it takes me to the default wordpress backend instead of to randomURL.com):

if (isset($user->roles) && is_array($user->roles) ) {
  if (in_array('member', $user->roles) && isset($_GET['action']) && $_GET['action'] == 'ppe' ) {
    return "www.randomURL.com";
  }
}

I thought that maybe it doesn't work because when the user logs in successfully, the only condition that is true is in_array('member', $user->roles); the reason is because after logging in, the url is no longer "https://mywebsite.com/wp-login.php?action=ppe". Ok, that makes sense.

So I decided to create variables in my function:

$isSet = isset($_GET['action']);
$actionName = $_GET['action'];

When I'm on "https://mywebsite.com/wp-login.php?action=ppe", I see the value of my variables when I log them to the console like this:

if ($isSet && ($actionName == 'ppe')) {
  echo "<script>console.log('isset = " . $isSet . ", actionName = " . $actionName .  "');</script>";
}

With those variables "saved", I tried this:

if (isset($user->roles) && is_array($user->roles) ) {
  if (in_array('member', $user->roles) && $isSet == 1 && $actionName == 'ppe' ) {
    return "www.randomURL.com";
  }
}

That also did not work. I was logged in to the backend instead of randomURL.com What am I doing wrong? Why is this not working?

NOTE: I created the "custom" login url and added it to a Navigational Menu item. I believe it's valid as I'm able to login and log out without issue. Also, all code written above is in my functions.php file.

My setup is like this:

function filterLoginRedirect($redirect_to, $request, $user) {
  // my code here
}

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

I've seen these posts (and several others), but none were helpful:

Redirect based on parameter in url

Redirect based on $_GET parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.