Adding the following code snippet into the functions.php
of your wordpress theme will allow you to redirect failed login attempts to any location.
/* Adding this snippet to the functions.php or your wordpress theme will allow you to redirect failed login attempts to any location. */
add_action( 'wp_login_failed', 'dcg_redirect_failed_login' );
function dcg_redirect_failed_login( $username ) {
$referrer = $_SERVER['HTTP_REFERER'];
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
wp_redirect( $referrer . '?login=failed' );
exit;
}
}
• View above code on Github Gist →
Howdy. Wouldn’t it be better to use a native WordPress function like wp_get_referer() instead of $_SERVER[‘HTTP_REFERRER’]? Also, $referrer should probably be sanitized there with esc_url_raw() or something like that, and/or use wp_safe_redirect() instead – just to be sure $referrer wasn’t spoofed.
Greg, Totally agree with you. Thanks for suggestions. ?