4

I use the following code to redirect users on login to dashboard instead of their profile.

It works fine but it also redirects to dashboard when there is redirect_to= in url http://example.com/wp-login.php?redirect_to=

Is possible when there is redirect_to= in url to redirect to the appended url and not to dashboard?

// Redirect to dashboard on login (instead of profile)
add_filter( 'login_redirect', 'app_login_redirect', 10, 3 );
function app_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 admin_url( 'index.php' );
        }
    } else {
        return $redirect_to;
    }
}
5

The second input argument $request, in your app_login_redirect() callback function, takes it's value from source:

isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';

so if I understand you correctly, you could just check if it's empty or not, if you need to know that within your logic parts.

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.