Fix generic error when creating a member with a duplicate email - #1405
Merged
y000yal merged 1 commit intoSep 10, 2026
Merged
Conversation
wp_insert_user() returns a WP_Error for an existing email or username, but MembersRepository::create() guarded on truthiness only. A WP_Error object is truthy, so it passed the guard and was used to build a WP_User, which yielded ID 0. The controller's `if ( $member->ID )` then failed with no else branch, returning null, and AJAX::create_member() fell back to its generic "unexpected error while saving the members data" message. Return the WP_Error from the repository and surface get_error_message() in both the admin and public create paths, so the admin sees the real reason. The public path rolls back first since it runs in a transaction.
Contributor
|
Build for ⬇️ Download user-registration-5.2.7.zip (8.1M) Installs directly via Plugins → Add New → Upload Plugin. |
y000yal
deleted the
1512-admin-create-member-duplicate-email-generic-error
branch
September 10, 2026 06:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Changes proposed in this Pull Request:
Adding a member from the admin with an email that already belongs to an existing user showed
Sorry! There was an unexpected error while saving the members data .instead of telling the admin the email was already in use.WordPress actually rejects the duplicate correctly and returns a clear, already-translated message. The plugin was discarding it.
Root cause
wp_insert_user()returns aWP_Errorfor an existing email or username.MembersRepository::create()guarded on truthiness only:A
WP_Erroris an object, so it is truthy. It passed the guard and was used to construct aWP_User, which is non-numeric and therefore resolved to ID0.MembersController::create_members_admin()then failed itsif ( $member->ID )check, had noelse, and fell off the end returningnull.AJAX::create_member()saw a response with nomessagekey and printed its generic fallback string.Runtime trace before the fix:
Changes
MembersRepository::create()— return theWP_Errorinstead of letting it become aWP_Userwith ID 0.MembersController::create_members_admin()— returnget_error_message()so the AJAX layer has a real message.MembersController::create_members_public()— same guard for frontend registration, which swallowed the error the same way. Rolls back first, since it runs inside a transaction.Duplicate username was broken by the same mechanism and is fixed by the same guards.
flowchart TD A["Admin submits new member<br/>with an existing email"] --> B["wp_insert_user()"] B --> C["returns WP_Error<br/>existing_user_email"] C --> D{"MembersRepository::create()"} D -->|"before: if ( $new_user_id )<br/>WP_Error is truthy"| E["new WP_User( WP_Error )<br/>ID = 0"] D -->|"after: is_wp_error() guard"| F["return the WP_Error"] E --> G["controller: if ( $member->ID )<br/>fails, no else"] G --> H["returns NULL"] H --> I["AJAX generic fallback<br/>'unexpected error while saving<br/>the members data'"] F --> J["controller: is_wp_error() guard<br/>returns get_error_message()"] J --> K["'Sorry, that email address<br/>is already used!'"] style I fill:#ffdddd,stroke:#cc0000,color:#000 style K fill:#ddffdd,stroke:#00aa00,color:#000 style E fill:#ffdddd,stroke:#cc0000,color:#000Closes wpeverest/user-registration-pro#1512
How to test the changes in this Pull Request:
Sorry, that email address is already used!Before this fix:
Sorry! There was an unexpected error while saving the members data .Expected:
Sorry, that username already exists!Types of changes:
Other information:
Verified locally against the real controller. Duplicate email now returns
Sorry, that email address is already used!, duplicate username returnsSorry, that username already exists!, and creating a brand-new member still succeeds.Noted, not fixed here (out of scope):
MembersRepository::update()has the same truthy-WP_Errorflaw, so editing a member to a duplicate email fails silently. Its only caller discards the return value entirely, so it needs the caller reworked too — worth a separate issue.Changelog entry