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?

Tim Plummer
  • 255
  • 1
  • 3
  • 10

2 Answers2

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.

Dipesh KC
  • 924
  • 1
  • 8
  • 20
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.

Pieter Goosen
  • 55,488
  • 23
  • 117
  • 211
MAK
  • 11
  • 1