Knowing when your WordPress users are active is helpful whether you open sign-ups to the public, or if you have a private site with multiple users. I’ll show you how to make a new column on your Users page in the WordPress admin as well as adding a Dashboard metabox with a “Currently Online” count of your online users.
WordPress Transients
In this tutorial, we’ll use a transient to store a temporary array of user login data that we can pull online users from. Transients are amazing and I would highly encourage all WordPress developers to use them if you don’t already. They will save lots of queries on your database by temporarily storing data in a cache for a certain amount of time. If they don’t exist, or have expired, they will simply return false
. For more information about how and when to use transients, read more here.
The following code snippets can all be placed inside of functions.php
.
Utility Functions
To make it easier on ourselves, and to keep the code DRY, we’ll first create some utility functions that will be called depending on what data we want to set or get for online users.
Storing the Data
First thing we need to do is create a transient that is updated whenever a user becomes active. We will hook into both the init
and admin_init
actions provided by WordPress because active users can be on both the front-end and back-end of the CMS.
Inside that function, we will get our transient (I know it sounds out of order if you’re not familiar with transients- Remember: transients will return false if they don’t exist or expired) as well as the current user’s ID.
Next, we’ll check the transient to see if the user exists or if they have not been online in the last 15 minutes.
If they need to be updated, we’ll update the transient array by appending to it with their ID as the index. Inside of this array, we’ll store their ID, username, and current time (aka, their “last seen” time).
Currently Online?
Next, we’ll create a function to simply check if a specific user is currently online. This will come in handy when we’re filling in the status of each user in the User admin listing.
All it is going to do is the same check as when we update the online users transient; it will return true or false whether the requested user is in the array and if their last online time is in the last 15 minutes.
Last Online?
Finally, we’ll make a function that simply returns when the specific user was last seen. To do this, we’ll pull the transient data and return the last online time if it exists, otherwise it will return false
.
Here are the utility functions code snippets:
PHP
WordPress Admin User List Custom Column
Now that we are obtaining the data, we can actually do something with it! Let’s start by creating a custom column on the WordPress Admin User page. To do this, hook into the manage_users_columns
filter to add a column head called “Status”.
Then we’ll hook into the action manage_users_custom_column
so we can fill it with our desired data. In this case, we’ll wait for the “status” column name and if the user is currently online (using our gearside_is_user_online
function) we’ll return an bold “Online Now” text in the column. Otherwise, we’ll either return the “Last Seen” date, or nothing if they have never logged in.
Here is the full custom user column snippet:
PHP
I like to create a custom User field for which company they work for so, and then add that data as another custom column. Now I can not only see who is currently online, but I also know quickly who is who when there are many people working on a single project.
Active User Count in WordPress Dashboard Metabox
Now let’s make a custom metabox for the WordPress Dashboard to show how many users are currently on the site. To do this, we’ll first hook into the wp_dashboard_setup
action to register our custom metabox. Then, we’ll tell the metabox which function to run inside of it.
Inside that function will be our HTML and call to the last big function we need to write. The HTML will just spit out a div with the total registered users and how many of them are active now.
Online Users Count
To get an integer of who is online, we just need to loop through the transient array and if that index’s last online time is within the last 15 minutes increment a counter. Then we’ll return that count at the end. The following snippet looks more in depth because I gave this function the ability to return an array of only those active users, too. So you could also get a list of who is active (or just the integer count).
Here is the full Active User metabox snippet:
PHP
In my starter WordPress theme, “Nebula“, (where this functionality is also implemented), I use a custom “At A Glance” metabox where the active user count is shown. For more ideas of what other information you could add to a custom Dashboard metabox check out the Nebula function under “At A Glance Metabox”!