WordPress stores user data in the
wp_users table, which is linked to the
WordPress also stores user data in two other tables:
- Additional meta data on users is stored in the
- Data on commenters who are not logged in is stored in the
In this part of my series on data in WordPress, I'll look at how WordPress stores user data and how you can access it, focusing first on the
The wp_users Table
wp_users table stores all of the core information about each user. It has the following fields:
|Field||What it stores||Notes|
||the user ID
||auto-generated if not provided at signup|
||auto-generated if not entered manually|
||date and time the user first registered
||user activation key
||status stored as a number - this tells WordPress whether the user has confirmed registration via email, for example. It does NOT store user roles.
||publicly displayed name
||auto-generated if not entered manually
As you can see, all but one field (
user_url) are either mandatory on signup or will be auto-generated.
User Meta Data
As well as the data in
wp_users, there is data which is created for all users but is stored in the
wp_usermeta table, such as roles and capabilities. This table is also used to store additional settings to enhance the user experience such as the chosen admin color scheme and settings for the admin bar and dashboard display.
This is the table you should use when you want to create additional fields for users via your theme or plugin - you should never add fields to the main
Each record in the
wp_usermeta table has four fields:
ID- the record ID
user_id- which is linked to
To create a new user meta data record, you use the
add_user_meta( $user_id, $meta_key, $meta_value, $unique );
The fourth parameter for this function (
$unique) is optional and specifies whether the value in the
meta_key field should be unique.
Once you've added user meta data, you can output it on each user's author page using
get_user_meta() or create a list of all users with a given value for a specified key.
I'll cover this last option in more detail in a later tutorial where I examine meta data and taxonomies.
Relationships Between Users and Other Content
Users can be linked to two content types: posts and comments. In the case of posts, there will always be a user, who will be the author. The relationship is between the
post_author field in
wp_posts and the ID field in
Comments do not always have a link to the
wp_users table: this will only be created if the commenter is a user who is logged-in. If this is the case, the link is between
If a commenter is not a user who is logged-in, then their details will be recorded directly to the
wp_comments table, using the
Users are fundamental to a WordPress installation. Without them you have no site administrator and no authors to create content.
WordPress stores core data about users in the
wp_users table and also uses the
wp_usermeta table for additional metadata. It also links user data to posts in the
wp_posts table and to comments in the
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post