• Resolved Riddhi Mehta

    (@riddhiehta02)


    Hello Team,

    I am usign your plugin and use the login authentication fro the same.

    Now I want to change the error message while user details are not valid.

    Can you please guide me how to customise that error message.

    {
        "code": "[jwt_auth] invalid_username",
        "message": "<strong>ERROR</strong>: Invalid username. <a href=\"link?action=lostpassword\">Lost your password?</a>",
        "data": {
            "status": 403
        }
    }

    In this I want to change the message. Please guide me how to do that.

    Thanks,
    Riddhi Mehta

    • This topic was modified 5 years, 6 months ago by Riddhi Mehta.
Viewing 1 replies (of 1 total)
  • Thread Starter Riddhi Mehta

    (@riddhiehta02)

    Hello All,

    I found the solution for the same. This plugin use the default WordPress error. so, you can use below code for change message in the API response. I got the success in this. Hope it’s help to you too!!

    remove_filter( 'authenticate', 'wp_authenticate_username_password' );
    add_filter( 'authenticate', 'wpse_115539_authenticate_username_password', 20, 3 );
    /**
     * Remove WordPress filer and write our own with changed error text.
     */
    function wpse_115539_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_username', __('<strong>ERROR</strong>: The username field is empty.' ) );
    
            if ( empty( $password ) )
                $error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) );
    
            return $error;
        }
    
        $user = get_user_by( 'login', $username );
    
        if ( !$user )
            return new WP_Error( 'invalid_username', sprintf( __( '<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?' ), 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( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?' ),
            $username, wp_lostpassword_url() ) );
    
        return $user;
    }

    For reference please refer this link: https://wordpress.stackexchange.com/questions/115539/custom-login-errors-and-variables-i-can-use

    Thanks,
    Riddhi Mehta

    • This reply was modified 5 years, 6 months ago by Riddhi Mehta.
    • This reply was modified 5 years, 6 months ago by Andrew Nevins.
Viewing 1 replies (of 1 total)
  • The topic ‘Customise error message’ is closed to new replies.