How can I say to all visitors that the admin is online. I mean if the admin is online all the visitors, registered and none registered, will see this words “hello the admin is online…talk with me”… ?
Thank you
How can I say to all visitors that the admin is online. I mean if the admin is online all the visitors, registered and none registered, will see this words “hello the admin is online…talk with me”… ?
Thank you
This should get you started. I’ve used a timeout of 5 minutes to allow for time sitting idle on the website. You could improve the accuracy with a script (if the current user is the admin) & pinging an AJAX request every few minutes to update the admin_last_seen
timestamp.
/**
* Check if the admin was last online at least 5 minutes ago.
*
* @return bool
*/
function wpse_140253_is_admin_online() {
if ( false === $last_seen = get_option( 'admin_last_seen' ) )
update_option( 'admin_last_seen', $last_seen = 0 );
elseif ( $last_seen )
return $last_seen + 5 * MINUTE_IN_SECONDS > time();
return false;
}
/**
* Update "admin_last_seen" timestamp.
*
* @link http://wordpress.stackexchange.com/q/140253/1685
*/
function wpse_140253_update_admin_online_status() {
if ( current_user_can( 'manage_options' ) /* Might be better to check user ID if you have multiple admins */ ) {
if ( ! $last_seen = get_option( 'admin_last_seen' ) )
$last_seen = 0;
// Only make a database update if last seen greater than timeout.
if ( $last_seen + 5 * MINUTE_IN_SECONDS < time() )
update_option( 'admin_last_seen', time() );
}
}
add_action( 'init', 'wpse_140253_update_admin_online_status' );
And in use:
<?php if ( wpse_140253_is_admin_online() ) : ?>
<div class="message">Admin is online!</div>
<?php endif ?>