I have made a login form on the front-end of my site using wp_login_form()
. However, when the user types in an incorrect username/password they are redirected to the wp-admin login form.
How can you instead display any error messages above the login form? Ideally, I don’t want the user to ever have to see the WordPress admin section.
I have tried using the redirect_invalid_login()
hook but this doesn’t seem to work?
1
just add the following code to your function.php.
it will redirect back user from where the login form is submitted.
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( $referrer . '?login=failed' ); // let's append some information (login=failed) to the URL for the theme to use
exit;
}
}