Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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 ?>
share|improve this answer
    
thank you a lot! –  yechiel Apr 4 at 14:21
    
For some reason it does not display the message ... Any idea what could be the problem? im checking it in a local server... is it metter? –  yechiel Apr 4 at 14:45
    
Woah, my bad. Bug was this line $last_seen = time(); - changed to $last_seen = 0; (see revised answer). –  TheDeadMedic Apr 4 at 15:32
    
now working !!! thanks a lot !!! –  yechiel Apr 6 at 6:58
1  
@yechiel Then please mark it as answer by ticking the check mark below the voting arrows on the left of that answer. –  kaiser Apr 8 at 23:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.