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?
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.
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.
wp_usermeta
or to create another table all together if that doesn't suffice.