How to use usermeta table with php

March 23, 2021

This will be a longer post and still such introduce usermeta.

One of the things I like about WordPress is the builtin user management features. Using phpMyAdmin, if you look at the wp_users table there’s not a lot there. Name, ID, email, the basics. But if you have the wp_usermeta you can see something special: potential! Actually depending on the number of users you have and what plugins you are using there could be all kinds of data. But the point of this blog is that with a few simple php commands you can exploit usermeta to store whatever data you’d like.

Again sticking with phpMyAdmin notice that unlike wp_users, this table is not set up with one user per row. Rather there are just 4 columns of interest:

  1. meta_id: the unique identifier for the row
  2. user_id: the id of the user to remind mysql who owns this piece of data
  3. meta_key: this is the name of the value. An given meta_value can be used only once per user.
  4. meta_value: this is the value

Here’s a simple example. In a study we had several hundred possible participants but only a few would be active at any given time. It was useful to know if a user was active or not so that the program wouldn’t waste resources checking various things. We added an “Active User” check box to the user profile page (with PODS>>link). this could be check manually or programmatically. To do so in the script was this easy:

            update_user_meta( $userID, 'activeuser', 1);

If the usermeta