Simple Ajax Chat: Auto-Clear Chats
This tutorial explains how to hook up automatic clearing of chat messages with my WordPress plugin, Simple Ajax Chat. Note that this tutorial is for the free version of SAC.
Auto-clear chat messages
To automatically clear/truncate chats every thirty minutes, add the following to your theme’s functions.php
file:
// define SAC auto-clear interval
function sac_truncate_chats_interval($interval) {
return 'thirty_minutes';
}
add_filter('sac_truncate_chats_interval_filter', 'sac_truncate_chats_interval');
SAC also includes the following built-in intervals:
sixty_minutes
thirty_minutes
three_minutes
thirty_seconds
So you can edit the return
value as needed, or add your own interval. Once an interval has been defined via your functions file, SAC automatically will reset the chat box. So if you never define an interval, SAC never will auto-clear messages.
Disabling auto-clear
If you define an interval and later change your mind, you can disable the auto-clear functionality by uninstalling and reinstalling the plugin, or use a cron plugin to manually delete the SAC cron interval. Also make sure to remove the interval definition, which we added in the previous section.
Going further
To go further, SAC also includes an action hook that fires whenever the chats are auto-cleared using WP Cron:
sac_truncate_chats_action
So for example, I use this hook for the following function:
// auto-clear chats notification
function sac_truncate_chats_notify($time, $truncate, $insert) {
$email = get_bloginfo('admin_email');
$subject = 'Cron Truncate Chatz';
$message = 'Time: ' . $time . "\n";
$message .= 'Truncate: ' . $truncate . "\n";
$message .= 'Insert: ' . $insert . "\n";
wp_mail($email, $subject, $message);
}
add_action('sac_truncate_chats_action', 'sac_truncate_chats_notify', 10, 3);
By adding that snippet to my theme’s functions.php
, I receive an email each time the chats are cleared. It helps me to keep an eye on the live SAC Demo.
Changing intervals
If you need to change the cron interval, from say thirty_minutes
to sixty_minutes
or whatever, deactivate/reactivate the plugin and/or use a cron plugin to manually remove/change the interval as needed. That is, just swapping out the interval in functions.php
isn’t enough — you need to remove the existing interval and then deactivate/reactivate the plugin to register the new interval.
Bonus tip
SAC also includes a hook for each chat message, so you can do stuff whenever someone leaves a chat:
sac_process_chat
That hook can be used like this, for example:
// chat message notifications
function sac_log_requests($sac_user_name, $sac_user_text, $sac_user_url) {
$site = get_bloginfo('name');
$email = get_bloginfo('admin_email');
$subject = 'New Chat Message at '. $site;
$message = 'Username: ' . $sac_user_name . "\n";
$message .= 'Message: ' . $sac_user_text . "\n";
$message .= 'User URL: ' . $sac_user_url . "\n";
wp_mail($email, $subject, $message);
}
add_action('sac_process_chat', 'sac_log_requests', 10, 3);
When added to your theme’s functions.php
file, this snippet will send an email alert containing the username, chat message, and user URL (if any) for each chat message.
Just some examples of cool things you can do with Simple Ajax Chat :)
Note: in older versions of SAC, the hook name is sac_process_chat_action
.