0

How to add data to a custom field at the wp_users table?

I already add a new field to the wp_users table but I don't know how to add the data to this field I want to use something like add_user_meta.

Any idea?

1
  • 3
    You don't want to modify the existing WordPress tables, because there is no guarantee that WordPress won't change something in the future that will completely collide with what you're doing. The correct way is, in fact, to store meta data in wp_usermeta or to create another table all together if that doesn't suffice.
    – NightHawk
    Commented May 7, 2018 at 21:48

2 Answers 2

1

You cannot add a field to the database table. You can only add a row or column, and neither are supposed to be added directly to the database, especially column!

With add_user_meta you are adding a new row but to the wp_usermeta table, and that should be sufficient for any extra required fields. This table is meant to be used for extending the user's profile with any amount of custom data, since it is key => value structured.

Again, if you add any column to the wp_users table, you can't access this data using native WordPress functions. You can only get it by writing a custom SQL query, but I don't think you actually need this.

1
  • A field is the same thing as a column
    – Chiwda
    Commented Oct 26, 2022 at 16:21
0

You're correct in using the add_user_meta() function. You can use it to pass the field name, value and the user id it is to be associated with.

Another option, if you are creating a meta box for custom information on the user profile page would be to use an existing library. Something like CMB2 will handle this really well.

https://github.com/CMB2/CMB2

2
  • Sorry but I think you can't understand me I already add the field into the database at the wp_users table the issue that I can't add the data to this field, add_user_meta function is used to add the meta to user not to add the data into wp_users
    – Yogu
    Commented Jun 22, 2017 at 11:05
  • How have you created the custom field for users? Most of the metadata for users, including custom fields is stored in the user_meta table.
    – Stephen
    Commented Jun 23, 2017 at 14:38

Your Answer

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

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