这应该让你开始。我已经使用了5分钟的超时时间来允许网站空闲。您可以使用脚本提高准确性(如果当前用户是管理员)(&;每隔几分钟ping一次AJAX请求以更新admin_last_seen
时间戳。
/**
* 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\' );
使用中:
<?php if ( wpse_140253_is_admin_online() ) : ?>
<div class="message">Admin is online!</div>
<?php endif ?>