Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active February 12, 2022 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisblakley/d85a8871f914ffe9d639 to your computer and use it in GitHub Desktop.
Save chrisblakley/d85a8871f914ffe9d639 to your computer and use it in GitHub Desktop.
Get data about active WordPress users on your site as well as list when they were last online.
<?php
/*==========================
This snippet shows how to add an active user count to the WordPress Dashboard.
Copy these contents to functions.php
===========================*/
//Active Users Metabox
add_action('wp_dashboard_setup', 'gearside_activeusers_metabox');
function gearside_activeusers_metabox(){
global $wp_meta_boxes;
wp_add_dashboard_widget('gearside_activeusers', 'Active Users', 'dashboard_gearside_activeusers');
}
function dashboard_gearside_activeusers(){
$user_count = count_users();
$users_plural = ( $user_count['total_users'] == 1 )? 'User' : 'Users'; //Determine singular/plural tense
echo '<div><a href="users.php">' . $user_count['total_users'] . ' ' . $users_plural . '</a> <small>(' . gearside_online_users('count') . ' currently active)</small></div>';
}
//Get a count of online users, or an array of online user IDs.
//Pass 'count' (or nothing) as the parameter to simply return a count, otherwise it will return an array of online user data.
function gearside_online_users($return='count'){
$logged_in_users = get_transient('users_status');
//If no users are online
if ( empty($logged_in_users) ){
return ( $return == 'count' )? 0 : false; //If requesting a count return 0, if requesting user data return false.
}
$user_online_count = 0;
$online_users = array();
foreach ( $logged_in_users as $user ){
if ( !empty($user['username']) && isset($user['last']) && $user['last'] > time()-900 ){ //If the user has been online in the last 900 seconds, add them to the array and increase the online count.
$online_users[] = $user;
$user_online_count++;
}
}
return ( $return == 'count' )? $user_online_count : $online_users; //Return either an integer count, or an array of all online user data.
}
<?php
/*==========================
This snippet contains utility functions to create/update and pull data from the active user transient.
Copy these contents to functions.php
===========================*/
//Update user online status
add_action('init', 'gearside_users_status_init');
add_action('admin_init', 'gearside_users_status_init');
function gearside_users_status_init(){
$logged_in_users = get_transient('users_status'); //Get the active users from the transient.
$user = wp_get_current_user(); //Get the current user's data
//Update the user if they are not on the list, or if they have not been online in the last 900 seconds (15 minutes)
if ( !isset($logged_in_users[$user->ID]['last']) || $logged_in_users[$user->ID]['last'] <= time()-900 ){
$logged_in_users[$user->ID] = array(
'id' => $user->ID,
'username' => $user->user_login,
'last' => time(),
);
set_transient('users_status', $logged_in_users, 900); //Set this transient to expire 15 minutes after it is created.
}
}
//Check if a user has been online in the last 15 minutes
function gearside_is_user_online($id){
$logged_in_users = get_transient('users_status'); //Get the active users from the transient.
return isset($logged_in_users[$id]['last']) && $logged_in_users[$id]['last'] > time()-900; //Return boolean if the user has been online in the last 900 seconds (15 minutes).
}
//Check when a user was last online.
function gearside_user_last_online($id){
$logged_in_users = get_transient('users_status'); //Get the active users from the transient.
//Determine if the user has ever been logged in (and return their last active date if so).
if ( isset($logged_in_users[$id]['last']) ){
return $logged_in_users[$id]['last'];
} else {
return false;
}
}
<?php
/*==========================
This snippet shows how to add a column to the Users admin page with each users' last active date.
Copy these contents to functions.php
===========================*/
//Add columns to user listings
add_filter('manage_users_columns', 'gearside_user_columns_head');
function gearside_user_columns_head($defaults){
$defaults['status'] = 'Status';
return $defaults;
}
add_action('manage_users_custom_column', 'gearside_user_columns_content', 15, 3);
function gearside_user_columns_content($value='', $column_name, $id){
if ( $column_name == 'status' ){
if ( gearside_is_user_online($id) ){
return '<strong style="color: green;">Online Now</strong>';
} else {
return ( gearside_user_last_online($id) )? '<small>Last Seen: <br /><em>' . date('M j, Y @ g:ia', gearside_user_last_online($id)) . '</em></small>' : ''; //Return the user's "Last Seen" date, or return empty if that user has never logged in.
}
}
}
@PlanetHacx
Copy link

i want your help sir i
want to display status of selected ids on other page of my website using you code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment