1

I have chosen to display a login form in the first page of my wordpress website. If the user logs in successfully, he gets redirected to a specific webpage. If he enters a wrong password, he gets redirected to the login page again. The problem happens when someone does not enter username and password: he gets redirected to the wordpress admin login page.

How can I redirect empty username logins back to the main login page ?

this is the code I am using to redirect failed attempts :

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

function _catch_login_error($redir1, $redir2, $wperr_user)
{
    if(!is_wp_error($wperr_user) || !$wperr_user->get_error_code()) return $redir1;

    switch($wperr_user->get_error_code())
    {
        case 'incorrect_password':
        case 'empty_password':
        case 'invalid_username':
        default:
            wp_redirect(home_url()); // modify this as you wish
    }

    return $redir1;
}
7

Check for an empty user login in the $_REQUEST array:

if ( empty( $_REQUEST['user_login'] ) // && or || empty( $_REQUEST['user_pass'] )
  return $redirect_to_whereever_you_want;

But this filter is to late. It is called after the user is logged in. You have to use the 'wp_authenticate' action, this is one of the earliest action in the login process:

add_action( 'wp_authenticate', '_catch_empty_user', 1, 2 );

function _catch_empty_user( $username, $pwd ) {
  if ( empty( $username ) ) {
    wp_safe_redirect( $redirect_to_whereever_you_want );
    exit();
  }
}

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.