Snippets: Redirect Users After Failed Login Attemp

function myprefix_redirect_after_login_fail( $username ) {
	// Check submission referrer
	$referrer = wp_get_referer();
	// If referrer is valid and it's not the default log-in screen redirect
	if ( ! empty( $referrer ) && ! strstr( $referrer, 'wp-login' ) && ! strstr( $referrer, 'wp-admin' ) ) {
		wp_redirect( add_query_arg( 'login', 'failed', $referrer ) );
		exit;
	}
}
add_action( 'wp_login_failed', 'myprefix_redirect_after_login_fail' );

Snippet Type: PHP

Posted Under: Other

All PHP snippets should be added via a child theme's functions.php file.