Last active
October 30, 2018 14:44
-
-
Save nfsarmento/a6ef94a1d9e33b6906b673258a6ce5c4 to your computer and use it in GitHub Desktop.
WordPress dashboard widget to show logged in users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates the new widget section to the administration dashboard if the user has the administrator user role. | |
* | |
* https://www.nuno-sarmento.com | |
*/ | |
add_action('wp_dashboard_setup', 'm1ka_online_users_widget'); | |
function m1ka_online_users_widget() { | |
if ( current_user_can( 'manage_options' ) ) { | |
wp_add_dashboard_widget( 'm1ka_online_users', 'Users Stats', 'm1ka_online_users' ); | |
} | |
} | |
/** | |
* Runs on each page load. | |
* If the user does not exist in the array in the 'online_status' transient, then add them. | |
*/ | |
function user_online_update(){ | |
if ( is_user_logged_in() ) { | |
// get the user activity the list | |
$logged_in_users = get_transient( 'online_status' ); | |
// get current user ID | |
$user = wp_get_current_user(); | |
// check if the current user needs to update his online status; | |
// he does if he doesn't exist in the list | |
// and if his "last activity" was less than let's say: 1 minutes ago | |
$no_need_to_update = isset( $logged_in_users[$user->ID] ) && $logged_in_users[$user->ID] > ( time() - ( 1 * 60 ) ); | |
// update the list if needed | |
if ( ! $no_need_to_update ) { | |
$logged_in_users[$user->ID] = time(); | |
set_transient( 'online_status', $logged_in_users, $expire_in = ( 1 * 60 ) ); // 2 mins | |
} | |
} | |
} | |
add_action( 'wp', 'user_online_update' ); | |
/** | |
* Runs when a user logs out. | |
* If the user exists in the 'online_status' array, remove them from there and update the transient. | |
*/ | |
function clear_transient_on_logout() { | |
$user_id = get_current_user_id(); | |
$users_transient_id = get_transient( 'online_status' ); | |
if ( is_array( $users_transient_id ) ) { | |
if ( $users_transient_id[$user_id] ) { | |
unset( $users_transient_id[$user_id] ); | |
set_transient( 'online_status', $users_transient_id, ( 1 * 60 ) ); // 2 mins | |
} | |
} | |
} | |
add_action( 'clear_auth_cookie', 'clear_transient_on_logout' ); | |
/** | |
* The contents of the widget | |
*/ | |
// Adds the content to the widget section | |
function m1ka_online_users() { | |
$logged_in_users = count(get_transient('online_status')); // gets number of logged-in users | |
$total_users = count_users(); // gets user info | |
echo '<div style="width: 48%; float: left; text-align: center;"><h1>'; | |
_e('Total Users: ', 'm1ka_ouw'); | |
echo $total_users['total_users']; | |
echo '</h1></div>'; | |
echo '<div style="width: 48%; float: left; text-align: center;"><h1>'; | |
_e('Online Users: ', 'm1ka_ouw'); | |
echo $logged_in_users; | |
echo '</h1></div><div style="clear: both;"></div>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment