I have a custom plugin that automatically creates a new user via wp_insert_user() if the current user does not exist yet.

We use data from our remote ldap server to provide the new user's details (user_login, user_email, etc.) for the wp_insert_user() function call.

This works without a hitch on most of our sites, but on a site that uses WP REST API to render the page, we have noticed that we are sometimes getting duplicate users in the database, same login name, email, etc. As far as I can tell, this shouldn't happen with wp_insert_user().

I am guessing that multiple async requests on the same page are triggering the wp_insert_user() function at virtually the same moment so that WordPress is not able to catch the duplicate entry before it is inserted. Is that possible? If so, would there be a way to always prevent duplicate inserts that resolve asynchronously?

  • Do they have the same user_login and email ?
    – birgire
    Aug 3 '17 at 16:28
  • Yeah, I do wp_insert_user( $userdata ) on init if the user doesn't exist, but that can be triggered more than once simultaneously if the site uses multiple REST API calls to build out the page. When this happens, these multiple wp_insert_user instances do their checks for an existing user_login and user_email early in the function, so they all pass and then they all do their insert action after the initial check has passed.
    – Matt
    Aug 4 '17 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.