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.
- The first block validates $_POST[email] and, if invalid, adds
invalid_emailbut 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', ... );
}
}
- The later block validates $user->user_email. Since the first block
did not assign a value, it is empty and the
empty_emailerror is added:
if ( empty( $user->user_email ) ) {
$errors->add( 'empty_email', ... );
} elseif ( ! is_email( $user->user_email ) ) {
...
}
Steps to Reproduce
- Go to Users > Add New (single site).
- Fill in "Username" with any value, e.g. "testuser".
- Fill in "Email" with a value that is not a valid address, e.g. "invalidemail" (no @).
- Leave the password field as generated.
- Click "Add User".
- 🐞 Bug occurs: two contradictory error messages are shown.
Expected Results
- Only the "invalid_email" error should be shown: Error: The email address is not correct.
- No "empty_email" error should appear, since the field was not empty.
Actual Results
- 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)
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
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Trac ticket:
https://core.trac.wordpress.org/ticket/66422
Use of AI Tools
AI assistance: Yes
Tool(s): DeepSeek
Used for: Drafting the test.