Redirecting a Failed Login Attempt

wp-login

wp-loginHere’s a nifty trick I helped a member with last week. If you manage membership sites for your clients (or yourself) this might just come in handy.

The problem: WishList Member provides a login widget for your sidebar, which works great. But if members use the wrong credentials (bad user name or password) they get dumped into the standard WordPress login, where the form shakes its head at you.

That’s not always what you want your members to see, so how can you capture that incorrect login and redirect the user to a more pleasant page? With a little bit of code in your functions.php file.

With credit to www.WPInsite.com, here’s a pretty elegant solution.

Caution: Always back up your functions file before editing, and if you’re editing it via the WordPress dashboard, make sure you have FTP or cPanel access to the site files. If you break it (and it’s easy to do) in the dashboard, you won’t be able to fix it there. You will have to use FTP to resolve it.

That said, here’s the code. Just copy and paste this into your functions.php file, and change the link after wp_redirect to the page you want the user to see.


add_action( 'wp_login_failed', 'my_front_end_login_fail' ); // hook failed login
function my_front_end_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from?
// if there's a valid referrer, and it's not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
wp_redirect( 'http://MYDOMAIN.COM/LOGIN-FAILED' ); //page to redirect to after login fails
exit;
}
}


Leave a Reply

Your email address will not be published. Required fields are marked *