最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论