問題描述
我需要顯示每個作者頁面的線上狀態 (線上/離線)(自定義作者頁面模板) 。
is_user_logged_in() 僅適用於當前使用者,我找不到針對當前作者的相關方法,例如 is_author_logged_in()
有任何想法嗎?
Answer
一個小技巧的小馬可以準備使用瞬態的二到三個功能的編碼,這是我以前沒有使用過的。
http://codex.wordpress.org/Transients_API
將其新增到 functions.php 中:
add_action('wp', 'update_online_users_status');
function update_online_users_status(){
if(is_user_logged_in()){
// 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 - (15 * 60)))){
$logged_in_users[$current_user] = $current_time;
set_transient('users_online', $logged_in_users, 30 * 60);
}
}
}
將其新增到 author.php(或另一個頁面模板) 中:
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') - (15 * 60)));
}
$passthis_id = $curauth->ID;
if(is_user_online($passthis_id)){
echo 'User is online.';}
else {
echo'User is not online.';}
第二個答案 (不要使用)
這個答案是為了參考。正如 One Trick Pony 所指出的,這是不希望的方法,因為資料庫在每個頁面的載入上被更新。經過進一步審查,程式碼似乎只是檢測當前使用者的 log-in 狀態,而不是將其與當前作者進行匹配。
1) 安裝此外掛:http://wordpress.org/extend/plugins/who-is-online/
2) 將以下內容新增到您的頁面模板中:
//Set the $curauth variable
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;
// Define the ID of whatever authors page is being viewed.
$authortemplate_id = $curauth->ID;
// Connect to database.
global $wpdb;
// Define table as variable.
$who_is_online_table = $wpdb->prefix . 'who_is_online';
// Query: Count the number of user_id's (plugin) that match the author id (author template page).
$onlinestatus_check = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM ".$who_is_online_table." WHERE user_id = '".$authortemplate_id."';" ) );
// If a match is found...
if ($onlinestatus_check == "1"){
echo "<p>User is <strong>online</strong> now!</p>";
}
else{
echo "<p>User is currently <strong>offline</strong>.</p>";
}
最佳解決方案
我會用 transients 來做到這一點:
-
建立一個你掛鉤在
init上的 user-online-update 功能; 它會看起來像這樣:// get the user activity the list $logged_in_users = get_transient('online_status'); // get current user ID $user = wp_get_current_user(); // check if the current user needs to update his online status; // he does if he doesn't exist in the list $no_need_to_update = isset($logged_in_users[$user->ID]) // and if his "last activity" was less than let's say ...15 minutes ago && $logged_in_users[$user->ID] > (time() - (15 * 60)); // update the list if needed if(!$no_need_to_update){ $logged_in_users[$user->ID] = time(); set_transient('online_status', $logged_in_users, $expire_in = (30*60)); // 30 mins }所以這應該在每個頁面的載入上執行,但是隻有在需要時才會更新。如果您有大量線上使用者,您可能希望增加”last activity” 時間框架以減少 db 寫入,但是對於大多數站點來說,15 分鐘是足夠的
-
現在要檢查使用者是否線上,只需檢視該瞬態內容,檢視某個使用者是否線上,就像上面所述:
// get the user activity the list $logged_in_users = get_transient('online_status'); // for eg. on author page $user_to_check = get_query_var('author'); $online = isset($logged_in_users[$user_to_check]) && ($logged_in_users[$user_to_check] > (time() - (15 * 60)));
如果沒有任何活動,短暫的時間將在 30 分鐘內到期。但是如果您的使用者線上時間一直不會過期,那麼您可能希望透過在 twice-daily event 上掛載另一個功能來定期清理瞬態。此功能將刪除舊的 $logged_in_users 條目…
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。