Mohamed Omar
Mohamed Omar

Reputation: 473

How to redirect logged in users conditionally?

Now i know that if i want to redirect logged in users to the current page they are already in i should use this filter

function pro_redirect_to_request( $redirect_to, $request, $user ){
// instead of using $redirect_to we're redirecting back to $request
return $request;
}
add_filter('login_redirect', 'pro_redirect_to_request', 10, 3);

but now i have a custom login page which i want to redirect users who are logging from this page to a custom page, i mean by that the homepage. So I made these edits to the filter but it doesn't work.

function pro_redirect_to_request( $redirect_to, $request, $user ){
// instead of using $redirect_to we're redirecting back to $request
if ($request == 'http://prosentra.com/login'){
    wp_redirect(home_url(""));
    exit();
}else{
return $request;
}
}
add_filter('login_redirect', 'pro_redirect_to_request', 10, 3);

Here i ask if the page that user requested logging in from equals this url 'http://prosentra.com/login' then redirect me to home page , and if not redirect me to the current page. What modifications should i do?

Upvotes: 0

Views: 301

Answers (2)

Vasim Shaikh
Vasim Shaikh

Reputation: 4532

The login_redirect filter is used to change the location redirected to after logging in

/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

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

Redirect all logins to the homepage with an anonymous function (php 5.3+).

add_filter( 'login_redirect', create_function( '$url,$query,$user', 'return home_url();' ), 10, 3 );

You can find reference here

https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect

Upvotes: 0

Tosin Onikute
Tosin Onikute

Reputation: 4002

Why don't you use HTTP_REFERER ? to check if users are coming from your custom login page.

So i just tested this with Wordpress now, It works.

function pro_redirect_to_request( $redirect_to, $request, $user ){

$prev_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

$request_url = "Your custom URL after login";
$default_url = "Your default URL"; 

if($prev_url == "http://prosentra.com/login/"){
   $redirect_to = $request_url;
} else {
   $redirect_to = $default_url;
}

return $redirect_to;

}
add_filter('login_redirect', 'pro_redirect_to_request', 10, 3);

I tested this with Woocommerce, It worked fine too.

function wc_custom_user_redirect( $redirect_to, $user ){

$prev_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

    $request_url = "Your custom URL after login";
    $default_url = "Your default URL"; 

if($prev_url == "http://prosentra.com/login/"){
   $redirect_to = $request_url;
} else {
   $redirect_to = $default_url;
}

return $redirect_to;

}
add_filter('woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 3);

Upvotes: 1

Related Questions