This is the 14th day of my participation in the More text Challenge. For more details, see more text Challenge

Understanding and Working with User Data in WordPress

Understand and use user data in WordPress

WordPress stores user data in the WP_Users table, which links to the WP_posts and WP_comments tables:

WordPress also stores user data in two other tables:

  • The user’s additional metadata is stored inwp_usermetaIn the table
  • The data of the unlogged users in the comments is stored inwp_commentsIn the table.

This current part of the WordPress data series will explore how WordPress stores and accesses user data, starting with the WP_Users table

Table wp_users

The WP_Users table stores the core information for each user. It contains the following fields:

field Store content instructions
ID User ID — The user ID Automatically generate
user_login User name – the username necessary
user_pass Password – the password If not provided during registration, it will be generated automatically
user_nicename A nickname – the nickname If there is no manual input, it is automatically generated
user_email Email address: Email address necessary
user_url Site – website Not necessary
user_registered Date and time when the user first registered Automatically generate
user_activation_key User activation code Auto-generated – a function that sends notification of user registration activation within the default user registrationwp_new_user_notificationTo callget_password_reset_keyGenerate the activation code and save it
user_status The status is stored as a number — for example, it tells WordPress whether the user confirmed the registration by email.Not used to store user roles Automatically generate
display_name The name of the public display Automatically generated when no manual input is required

As you can see, with the exception of the user_URL field, all fields are either required at registration or automatically generated.

User metadata

In addition to the data in WP_Users, there is data created for all users but stored in the WP_UserMeta table, such as Roles and Capabilities. The table is also used to store other Settings to enhance the user experience, such as the selected administrative background color scheme and Settings for the administration bar and dashboard display.

The WP_UserMeta table should be used when you need to create additional fields for users through your theme or plugin — and should never add fields to the main WP_Users table.

Each record in the Wp_userMeta table has four fields:

  • ID— ID of the record
  • user_id— which Wp_users to link to
  • meta_key
  • meta_value

Create a new user metadata record using the add_user_meta() function:

add_user_meta( $user_id.$meta_key.$meta_value.$unique );
Copy the code

The fourth argument ($UNIQUE) to this function is optional and specifies whether the value in the meta_key field should be unique.

Or use the update_user_meta function.

After adding the user metadata, you can output each user on the author page using get_user_meta() or create a list of all users with a specified key value.

In later tutorials, I’ll look at metadata and taxonomies to cover this last option in more detail.

The relationship between users and other content

Users can link to two content types: posts and comments.

In the case of Posts, there is always a user who is the author. This relationship is between the POST_AUTHOR field of WP_posts and the ID field of WP_Users.

Comments are not always linked to the WP_Users table: they are created only if the reviewer is a logged in user. In this case, the link is between the ID of Wp_Users and the user_ID of Wp_comments.

If the reviewer is not a logged in user, the information is recorded directly in the WP_comments table through the COMMENT_author, COMMENT_author_email, COMMENT_author_url, and comment_author_IP fields.

The profile

The user is the foundation of the WordPress installation. Without them, there would be no site administrators and no authors to create the content.

WordPress stores core data about users in the WP_Users table and other metadata in the WP_UserMeta table. It also links user data to posts in the WP_Posts table and comments in the WP_Comments table.