WebDevBooster
WebDevBooster

Reputation: 14954

Where is the "Invalid username" WooCommerce error message coming from?

I need to modify the text of a WooCommerce error message (for security reasons) but can't seem to find where this particular message is actually coming from.

I'm on the example.com/shop/my-account/ page (could have been example.com/my-account/ as well) and purposefully entering wrong username and password for testing.

The relevant HTML part looks exactly like this:

<ul class="woocommerce-error" role="alert">
    <li>
        <strong>ERROR</strong>
        : Invalid username.
        <a href="http://example.com/shop/my-account/lost-password/">Lost your password?</a>
    </li>
</ul>

I need to change the Invalid username part of the message to something else.

So, I searched for that exact string but can only find instances of Invalid username or email. which obviously does NOT match the error message I'm trying to modify.

I then checked to see where is the entire HTML part being generated and found that it's being generated in plugins\woocommerce\templates\notices\error.php.

Here's the relevant part:

<ul class="woocommerce-error" role="alert">
    <?php foreach ( $messages as $message ) : ?>
        <li><?php echo wp_kses_post( $message ); ?></li>
    <?php endforeach; ?>
</ul>

So, that error message is coming from a $messages array.

But WHERE can I find the actual message in question?

(so that I can modify it)

Why is the exact match (Invalid username) nowhere to be found?

I tried to search for $messages, of course, but there's nothing relevant to be found. So, where the heck is that particular error message actually coming from? Even searching in the entire wp-content folder doesn't lead anywhere.

Upvotes: 2

Views: 4030

Answers (3)

Muhammad Maaz
Muhammad Maaz

Reputation: 1

I used the above snippets but its doesn't work for me then I tried this one.

function no_wordpress_errors(){
  return 'Invalid credentials';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

Its working for me

Upvotes: 0

benji
benji

Reputation: 1

I know this post is old, but in case it can help someone I think that the piece of code you need to modify is located in the function "process_login()" located in the file "woocommerce/includes/class-wc-form-handler.php" of Woocommerce plugin:

if ( is_wp_error( $user ) ) {
    $message = $user->get_error_message();
    $message = str_replace( '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', $message );
    throw new Exception( $message );
} else {

You can change the message before the exception is thrown.(by the way I do not understand the added value of the str_replace since it does nothing).

Comments are welcome.

Upvotes: 0

Justin R.
Justin R.

Reputation: 488

I believe, the actual message in question comes from WordPress itself and not WooCommerce. Specifically try looking in wp-includes/user.php. If you want to change the message, I would strongly recommend using a hook to do so to prevent losing your modification when WP is updated.

Upvotes: 1

Related Questions