WordPress.org

Ready to get started?Download WordPress

Forums

wp_update_user vs wp_insert_user (3 posts)

  1. gilzow
    Member
    Posted 3 years ago #

    Have a plugin that I manage where I have been using wp_update_user to both create new users and update existing users. I've had a couple of users inform me that my plugin is failing at the point where it should be creating a new user via wp_update_user. If I have them change it to wp_insert_user then it functions just fine. In looking at registration.php it appears that the ONLY advantage of using wp_update_user is if you are updating a user's password.

    Is this assumption correct? If so, can anyone think of a reason I shouldnt go ahead and switch to using wp_insert_user?

    Also, can anyone think of a reason that wp_update_user would fail but wp_insert_user wouldnt when the array of user data passed to both of them is identical?

  2. Tim Moore
    Moderator
    Posted 3 years ago #

    What does the data array look like when a user needs to be created? If the ID is being set, even if empty, WordPress will assume it is updating a user and then fail because the ID is empty. wp_insert_user is likely ignoring the ID variable, or setting it as usermeta.

  3. gilzow
    Member
    Posted 3 years ago #

    data array contains

    • user_pass
    • user_login
    • user_email
    • nickname
    • display_name
    • role

    Specifically this is the array that is being handed to the wp_insert_user function from within wp_update_user

    array ( 0 => '', 'user_pass' => 'XXX', 'user_login' => 'somename', 'user_email' => 'some@email.com', 'nickname' => 'somenick', 'display_name' => 'some name', 'role' => 'editor', )

    The 0 key is set by the wp_update_user function due to passing a false value to add_magic_quotes (line 1552).

    and this is after I run checks on user_name and user_email via sanitize_user(), username_exists() and email_exists(). I've walked through all of the wordpress code in wp_update_user and the various functions it calls and cant see where it would fail versus using wp_insert_user directly.

Topic Closed

This topic has been closed to new replies.

About this Topic