Want to check if a user (not current user) is logged in? Use following code in your theme’s functions.php file OR in site specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // 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 $no_need_to_update = isset($logged_in_users[$user->ID]) // and if his "last activity" was less than let's say ...15 minutes ago && $logged_in_users[$user->ID] > (time() - (15 * 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 = (30*60)); // 30 mins } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.