Make WordPress Core

Opened 5 days ago

Last modified 5 days ago

#66422 new enhancement

Single site: Creating user with invalid email shows contradictory empty_email and invalid_email errors

Reported by: jorgelopezrepiso Owned by:
Priority: low Milestone: Awaiting Review
Component: Users Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Creating a new user on a single site with a non-empty but invalid email address (e.g. "invalidemail") shows two contradictory error messages for the same field:

Error: The email address is not correct.
Error: Please enter an email address.

The first message correctly reports that the email is invalid. The second is incorrect: the field was not empty.

Root cause

When a new user is created on a single site in the admin, wp-admin/user-new.php calls the edit_user() function. In edit_user() (wp-admin/includes/user.php), the email is validated twice from different sources.

  1. The first block validates $_POST[email] and, if invalid, adds invalid_email but does not assign the value to $user->user_email:
if ( isset( $_POST['email'] ) ) {
    $maybe_email = wp_unslash( $_POST['email'] );
    if ( is_string( $maybe_email ) && is_email( $maybe_email ) ) {
        $user->user_email = $maybe_email;
    } else {
        $errors->add( 'invalid_email', ... );
    }
}
  1. The later block validates $user->user_email. Since the first block did not assign a value, it is empty and the empty_email error is added:
if ( empty( $user->user_email ) ) {
    $errors->add( 'empty_email', ... );
} elseif ( ! is_email( $user->user_email ) ) {
    ...
}

Steps to Reproduce

  1. Go to Users > Add New (single site).
  2. Fill in "Username" with any value, e.g. "testuser".
  3. Fill in "Email" with a value that is not a valid address, e.g. "invalidemail" (no @).
  4. Leave the password field as generated.
  5. Click "Add User".
  6. 🐞 Bug occurs: two contradictory error messages are shown.

Expected Results

  1. Only the "invalid_email" error should be shown: Error: The email address is not correct.
  2. No "empty_email" error should appear, since the field was not empty.

Actual Results

  1. Two contradictory errors are shown: Error: The email address is not correct. Error: Please enter an email address.

Additional Notes

The user's email is validated in two different places, while a single check would suffice. I'm attaching a PR with a possible solution. It also unifies all email validation logic within the function.

Attachments (1)

screenshot.jpeg​ (47.4 KB ) - added by jorgelopezrepiso 5 days ago.

Download all attachments as: .zip

Change History (2)

This ticket was mentioned in ​PR #15255 on ​WordPress/wordpress-develop by ​@jorgelopezrepiso.


5 days ago
#1

  • Keywords has-patch has-unit-tests added; needs-patch removed

Trac ticket:
https://core.trac.wordpress.org/ticket/66422

Use of AI Tools

AI assistance: Yes
Tool(s): DeepSeek
Used for: Drafting the test.

Note: See TracTickets for help on using tickets.