Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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();
  }
}
share|improve this answer
    
perfect, perfect, perfect. Thank you. –  RobDel Apr 28 '13 at 15:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.