Snippets: Redirect Users After Failed Login Attemp

function myprefix_redirect_after_login_fail( $username ) {
	// Check submission referrer
	$referrer = $_SERVER['HTTP_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( $referrer . '?login=failed' );
		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.