Understanding and Working with User Data in WordPress

WordPress stores user data in the wp_users table, which is linked to the wp_posts and wp_comments tables:

WordPress also stores user data in two other tables:

  • Additional meta data on users is stored in the wp_usermeta table
  • Data on commenters who are not logged in is stored in the wp_comments table.

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 wp_users table.

The wp_users Table

The wp_users table stores all of the core information about each user. It has the following fields:

Field What it stores Notes
ID
the user ID
auto-generated
user_login
username
required
user_pass
password
auto-generated if not provided at signup
user_nicename
nickname
auto-generated if not entered manually
user_email
email address
required
user_url
website
not required
user_registered
date and time the user first registered
auto-generated
user_activation_key
user activation key
auto-generated
user_status
status stored as a number - this tells WordPress whether the user has confirmed registration via email, for example. It does NOT store user roles.
auto-generated
display_name
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 wp_users table.

Each record in the wp_usermeta table has four fields:

  • ID - the record ID
  • user_id - which is linked to wp_users
  • meta_key
  • meta_value

To create a new user meta data record, you use the add_user_meta() function:

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 wp_users.

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 ID in wp_users and user_ID in wp_comments.

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 comment_author, comment_author_email, comment_author_url and comment_author_IP fields.

Summary

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 wp_comments table.

Tags:

Comments

Related Articles