1

I'm having a little trouble with a custom login form that I've created.

<?php if (!(current_user_can('level_0'))){ ?>
  <div class="login-form">
    <form action="<?php echo get_option('home'); ?>/wp-login.php" method="post" target="_blank">
      <p>
        <label for="log" id="user">Username</label>
    <input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20" />
      </p>
      <p>
        <label for="password" id="pass">Password</label>
        <input type="password" name="pwd" id="pwd" size="20" />
      </p>
        <input type="submit" name="submit" value="Login" class="button" />
      <p>
        <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label>
        <input type="hidden" name="redirect_to" value="<?php bloginfo('url');?>/login-tournament/tournaments/"/>
     </p>
    </form>
  </div>
  <div id="recovertext"><a href="<?php echo get_option('home'); ?>/wp-login.php?    action=lostpassword">Recover password</a></div>
  <div id="loginbox"><a href="<?php bloginfo('url'); ?>/register">Not registered yet?</a></div>
<?php } else { ?>
  <p class="login-text">You're already logged in.<br>Click here to view the <a href="http://MY-CUSTOM-REDIRECT-URL.com" target="_blank">Tournaments</a> page or you can <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout"> Logout</a></p>
<?php }?>

A successful login will redirect the user to a custom URL and open the custom url in a new window. (this is working fine)

The problem I have is that if the user enters the incorrect credentials, they are redirected to the default wp-login.php form. I need the user to stay on the current page and display an error message. I did find this snippet to prevent the redirect and it does work but it opens up the same page in a new window. Is it possible to prevent the new window opening when the incorrect details are entered. (I still need the new window to open on successful logins)

Further to this, if no login details are entered at all when the login button is clicked, it will also send the user to the default wp-login.php page. Is it possible to prevent this also?

1
  • is anyone able to offer any advice on this?
    – Brad
    Commented May 6, 2013 at 8:47

1 Answer 1

3

Had the same issue. This fixed it for me.

add_action( 'wp_login_failed', 'my_front_end_login_fail' );
function my_front_end_login_fail( $username ) {
     $referrer = $_SERVER['HTTP_REFERER'];
     if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')     ) {
          wp_redirect( $referrer . '?login=failed' );
          exit;
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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