The current state of inserting user information into a WordPress database


Writing PHP code is not easy for me. Easy ideas can be frustrating to complete. For example, inserting a new user into WordPress should be as simple as using the function wp_insert_user. An array is built and the information is inserted into the WordPress database. Only if it was that easy.

The XenWord plugin requires the ID of the user to match the XenForo member user_id. This means part of the array inserted into the WordPress database must be the ID. The WordPress developer site states the integer ID may be included in the array but the example provided on the page returns the newly created user_id. Most important, the page states, “If there is no ID, a new user will be created. If you pass an ID , the user with that ID will be updated.”

Since no user with that ID exists and the ID must be passed in the array, then wp_insert_user isn’t the best choice for adding the user information. Instead, an array is created and this information is added to the database by an insert command.

Once the information is added to the database then the role for the new user is added with the function set_role. Any meta information is added using the update_user_meta.

[pastacode lang=”php” path_id=”91c07754df8fe2580abb84586ce1b93a” file=”” highlight=”” lines=”” provider=”gist”/]

This is the only way I’ve found that works. Please let me know if you discover a better way to insert a new user into the database while passing the ID parameter.