13

I'm looking at the docs for add_user_meta() vs update_user_meta().

If the current meta_key does not exist for a user, will update_user_meta() automatically add the meta_key for that user or do you have to define the meta_key with add_user_meta() first?

4

2 Answers 2

22

You have already found out that using update_user_meta() if the meta field for the user does not exist, it will be added. ie update_user_meta() can do the task of add_user_meta()

However, the difference between them is the return values

update_user_meta()

returns False if no change was made (if the new value was the same as previous value) or if the update failed, umeta_id if the value was different and the update a success.

NOTE: as of v3.4.2 it returns the umeta_id on success (instead of true) and false on failure


add_user_meta()

return Primary key id for success. No value (blank) for failure. Primary key id for success.

1
  • 1
    I wanna add that the add_user_meta cannot update existing user_meta if the unique flag passed in is set to true. Mar 14, 2018 at 15:14
1

Thanks for your explanation, i will just add to yours the update_user_meta() also return true if successful update happened.

In short - It returns Meta ID if the key didn't exist, true on successful update, false on failure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.