One Trick Pony posted a great script that I'm trying to adapt to my needs.

Registration in my website is restricted. There is only one user, a editor. The idea is to have a Chat Plugin, and it would be enabled and disabled based on if the editor is online or not, since he will be answering the chat, and if the chat is enabled and editor offline, it only consumes space.

The code:

define('EDITOR_USER_ID', 2); // your editor user ID here

add_action('wp', 'update_online_users_status'); 

function update_online_users_status(){

    // get the online users list

    if(($logged_in_users = get_transient('users_online')) === false) $logged_in_users = array();

    $current_user = wp_get_current_user();
    $current_user = $current_user->ID;  
    $current_time = current_time('timestamp');

    if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - ($time_limit * 60)))){

      $logged_in_users[$current_user] = $current_time;

      set_transient('users_online', $logged_in_users, 1 * 60);


add_action('admin_init', 'handle_chat');

function handle_chat(){



     } else {




function is_user_online($user_id) {

  // get the online users list
  $logged_in_users = get_transient('users_online');

  // online, if (s)he is in the list and last activity was less than 15 minutes ago
  return isset($logged_in_users[$user_id]) && $logged_in_users[$user_id] > (current_time('timestamp') - (1 * 60));

Issues still left: Chat enabling/disabling should be instant, without having to wait 15 minutes after editor logout/login, so I've decreased this value to 1

Should be init instead of admin_init, so it could be enabled in the front end. But when I change it a errors shows up:

Fatal error: Call to undefined function is_plugin_active() in /home/mgdownhill/www/wp-content/themes/criatweb/functions.php on line 412

412-> if(!is_plugin_active('online-chat/online-chat.php')) activate_plugins('online-chat/online-chat.php');

My question: Is there a way to make it instant, without overloading the server? I will have a lot of websites running this chat script in the same server.

  • just a note: the $time_limit variable from the first function above is not defined, so you should replace that with 1 or whatever is your time limit – onetrickpony Feb 10 '13 at 20:28
  • /\ Done! PS: To add it to the front end, I have to change "admin_init" to "init", and add a line like? include('../../wp-admin/plugins.php'); – Lucas Bustamante Feb 10 '13 at 20:41
  • require_once('wp-admin/includes/plugin.php'); Works on front-end but crashes on back-end – Lucas Bustamante Feb 10 '13 at 20:49
  • require_once (ABSPATH . '/wp-admin/includes/plugin.php'); - I think it's working now :D – Lucas Bustamante Feb 10 '13 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.