簡體   English   中英

我如何在wordpress中獲取登錄用戶的用戶對象

[英]how can i get user object of logged in users in wordpress

我正在嘗試將用戶重定向到特定頁面。 它適用於單個用戶,但我也想根據他們的登錄信息將每個用戶重定向到特定頁面。

我如何編輯此代碼$user_info = get_userdata(9100008); 獲取當前登錄的用戶user_data ?。

add_filter('woocommerce_login_redirect', 'wc_login_redirect');

function wc_login_redirect( ) {

  $user_info = get_userdata(9100008);
  //$user_info = get_current_user_id();
  //$user_id = get_current_user_id(); 

  if ($user_id == 0) {
    $redirect_to = 'http://example.com/'.$user_info->user_login.'/';
    return $redirect_to;
  }
 }

有一個專門用於此目的的函數,稱為get_current_user_id() (出於某種原因,它在您的代碼中被注釋掉了)。

在 Codex 中閱讀更多內容。

如果您需要當前用戶 OBJECT(正如您的代碼所暗示的那樣),您可以使用wp_get_current_user()

global $current_user;
//example for id;
$id=$current_user->ID;

或者

$data=get_currentuserinfo();

片段

add_filter('woocommerce_login_redirect', 'wc_login_redirect');

function wc_login_redirect($redirect_to ) {
global $current_user;
$id=$current_user->ID;

  if ($id == 0)  // this makes no sense, because this will never be true. Should probably be $id!=0
  {
    $redirect_to = 'http://example.com/'.$current_user->user_login.'/';
  }
    return $redirect_to;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM