TransWikia.com

custom error message for empty username and password using authenticate filter not working

WordPress Development Asked by Zian Tap Chan on October 30, 2021

I’m trying to override the authenticate to edit the default error message.

/**
Purposed: Custom Login Error Message
Description: This function override the default error message on login form.
**/
remove_filter( 'authenticate', 'wp_authenticate_username_password' );
add_filter( 'authenticate', 'custom_authenticate_username_password', 30, 3 );
/**
 * Remove WordPress filer and write our own with changed error text.
 */
function custom_authenticate_username_password( $user, $username, $password ) {
    if ( is_a($user, 'WP_User') )
        return $user;

    if ( empty( $username ) || empty( $password ) ) {
        if ( is_wp_error( $user ) )
            return $user;
        
        $error = new WP_Error();
        if ( empty( $username ) )
            $error->add('empty_email', __('The username or email field is empty.'));

        if ( empty( $password ) )
            $error->add('empty_password', __( 'The password field is empty' ));

        return $error;
    }

    $user = get_user_by( 'login', $username );

    if ( !$user )
        return new WP_Error( 'invalid_username', sprintf( __( 'Invalid username or email address.' ), wp_lostpassword_url() ) );

    $user = apply_filters( 'wp_authenticate_user', $user, $password );
    if ( is_wp_error( $user ) )
        return $user;

    if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) )
        return new WP_Error( 'incorrect_password', sprintf( __( 'The password you've entered is incorrect.' ),
        $username, wp_lostpassword_url() ) );

    return $user;
}

Unfortunately, the empty username or password error is not overriding.

The default error message for username and password are;
<strong>Error</strong> : The username field is empty.
<strong>Error</strong> : The password field is empty.

I would like to change it to;
The username or email field is empty.
The password field is empty.

However, the invalid_username and incorrect_password are working and I successfully override it.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP