login_redirect filter without overiding the redirect_to url parameter

35 Views Asked by At

What i'm trying to achieve is:

If user is one of two roles redirect him to specific page.

Else redirect to default redirect.

But if in the loginpage there was a redirect_to parameter in the url, use this redirect url in any case.

So I tried this:

function my_login_redirect($redirect_to, $request, $user)
{
    // Get the current user's role
    $user_role = $user->roles[0];

    // Get the value of the redirect_to parameter
    $custom_redirect = $_GET["redirect_to"];

    //is there a user to check
    if (isset($user->roles) && is_array($user->roles)) {
        //check for roles and that there is no custom redirect in url
        if (($user_role == "partner" || $user_role == "team_member") && empty($custom_redirect)) {
            return site_url("welcome-user/?test=".$custom_redirect);
        }
    } else {
        return $redirect_to;
    }
}

add_filter("login_redirect", "my_login_redirect", 10, 3);

(and a few other options) But it doesn't work.

Any ideas? Thanks.

0

There are 0 best solutions below