2015-08-24 19 views
0

嘗試在某人登錄WordPress後訪問插件中的用戶標識,以便我可以將他們在會話中執行的操作分配給自定義表中的該用戶標識。嘗試在WordPress登錄後訪問用戶ID

我有這樣的:

add_action('wp_authenticate', 'orr_gamify_save_session_to_user'); 

我也想嘗試:

add_action('wp_login', 'orr_gamify_save_session_to_user'); 

但是,這也不能工作。

然後函數是:

function orr_gamify_save_session_to_user() { 
    global $wpdb,$current_user; 

    if(!session_id()) 
     { 
     session_start(); 
     } 

    $UserID = $DataToUpdate[UserID] = get_current_user_id(); 
    $SessionID = $WhereToUpdate[SessionID] = session_id(); 

    $TableName = "wp_gamify"; 
    if($UserID != 0) 
     { 
     if($wpdb->update($TableName,$DataToUpdate,$WhereToUpdate)) 
      { 
      #print '<div class="alert alert-success fade in"><strong>Attempting...</strong> User '.$UserID.' for Sesssion '.$SessionID.'</div>'; 
      #print '<div class="alert alert-success fade in"><strong>Success!</strong> We\'ve assigned your progress to your account.</div>'; 
      } 
     else 
      { 
      #print '<div class="alert alert-danger fade in"><strong>Failed!</strong> We were unable to save your progress to your account.</div>'; 
      } 
     } 
} 

它更新,​​但它給了我零用戶名。

+0

所以你的函數get被觸發了,數據庫得到了更新,但是變量UserID = 0? – TeeDeJee

回答

1

wp_login如果您在add_action函數中定義參數的數量,則接受2個參數。 10表示參數的優先級,2表示參數的數量。

add_action('wp_login', 'orr_gamify_save_session_to_user', 10, 2); 

$ user是WP_User類。所以你可以使用它的ID。

function orr_gamify_save_session_to_user($user_login, $user) { 
    $UserID = $DataToUpdate["UserID"] = $user->ID; 

不要忘記引號來定義數組的鍵。

$UserID = $DataToUpdate["UserID"] = $user->ID; 
$SessionID = $WhereToUpdate["SessionID"] = session_id(); 
+0

謝謝TeeDeJee!我試圖在orr_gamify_save_session_to_user函數中執行2個參數,但它給了我一個錯誤。我能夠使$ user全球化,然後以這種方式拉動它。 也感謝提示使用引號來定義數組的關鍵。 –

+0

@詹姆斯我忘記了我的代碼中的一部分。我用優先級和參數更新了add_action部分。然後它會接受我的代碼。你可以試試看,讓我知道嗎? – TeeDeJee

相關問題