Skip to content

Fix generic error when creating a member with a duplicate email - #1405

Merged
y000yal merged 1 commit into
release/v5.2.8from
1512-admin-create-member-duplicate-email-generic-error
Sep 10, 2026
Merged

Fix generic error when creating a member with a duplicate email#1405
y000yal merged 1 commit into
release/v5.2.8from
1512-admin-create-member-duplicate-email-generic-error

Conversation

@saurab018

Copy link
Copy Markdown
Contributor

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 a WP_Error for an existing email or username. MembersRepository::create() guarded on truthiness only:

$new_user_id = wp_insert_user( $data['user_data'] );
if ( $new_user_id ) {          // a WP_Error object is truthy — passes
    $user = new \WP_User( $new_user_id );

A WP_Error is an object, so it is truthy. It passed the guard and was used to construct a WP_User, which is non-numeric and therefore resolved to ID 0. MembersController::create_members_admin() then failed its if ( $member->ID ) check, had no else, and fell off the end returning null. AJAX::create_member() saw a response with no message key and printed its generic fallback string.

Runtime trace before the fix:

wp_insert_user_returns     = WP_Error(existing_user_email): "Sorry, that email address is already used!"
repository_create_returns  = WP_User with ID = 0
controller_returns         = NULL
message_shown_to_admin     = "Sorry! There was an unexpected error while saving the members data."

Changes

  • MembersRepository::create() — return the WP_Error instead of letting it become a WP_User with ID 0.
  • MembersController::create_members_admin() — return get_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:#000
Loading

Closes wpeverest/user-registration-pro#1512

How to test the changes in this Pull Request:

  1. Note the email address of any existing user on the site.
  2. Go to User Registration & Membership → Memberships → Members → Create New Member.
  3. Fill in a new, unused username, and paste the existing user's email into the email field. Pick any membership and save.
  4. Expected: Sorry, that email address is already used!
    Before this fix: Sorry! There was an unexpected error while saving the members data .
  5. Repeat with an existing username and a fresh unused email.
    Expected: Sorry, that username already exists!
  6. Regression — create a member with a genuinely new username and email. It must still be created successfully and appear in the Members list.
  7. Frontend regression — register through a form that includes a membership field, once with a duplicate email (should now report the real reason, and must not leave a half-created user behind) and once with fresh details (must still succeed).

Types of changes:

  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (modification of the currently available functionality)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you successfully ran tests with your changes locally?
  • Have you updated the documentation accordingly?

Verified locally against the real controller. Duplicate email now returns Sorry, that email address is already used!, duplicate username returns Sorry, 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_Error flaw, 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

Fix - Show the actual reason instead of a generic error when creating a member with an email or username that is already in use.

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.
@saurab018 saurab018 self-assigned this Sep 1, 2026
@tg-autopilot

Copy link
Copy Markdown
Contributor

Build for 421047f2 is ready 🛎️

⬇️ Download user-registration-5.2.7.zip (8.1M)

Installs directly via Plugins → Add New → Upload Plugin.
Link expires in 30 days · updated Sep 1, 2026 8:14 AM +0545

@saurab018
saurab018 changed the base branch from develop to release/v5.2.8 September 10, 2026 05:27

@y000yal y000yal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@y000yal
y000yal merged commit f5e2d14 into release/v5.2.8 Sep 10, 2026
2 of 3 checks passed
@y000yal
y000yal deleted the 1512-admin-create-member-duplicate-email-generic-error branch September 10, 2026 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants