WordPress handles login failed in two ways:
- If it is a bad credential, and both username and password have a
value, then this action can be captured bywp_login_failed
- If both, or one, of the options are empty, then WordPress generates
the error object as the first parameter in the authenticate filter;
it does not open andwp_login_failed
action captures this
cause/event
For what we have done here, see comments in code:
add_filter( 'authenticate', function( $user, $username, $password ) {
// forcefully capture login failed to forcefully open wp_login_failed action,
// so that this event can be captured
if ( empty( $username ) || empty( $password ) ) {
do_action( 'wp_login_failed', $user );
}
return $user;
}, 10, 3 );
// to handle even you can handle the error like
add_action( 'wp_login_failed', function( $username ) {
if ( is_wp_error( $username ) ) {
// perform operation on error object for empty error
}
} );