简体   繁体   English

我如何在wordpress中获取登录用户的用户对象

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

I am trying to redirect users to specific page.我正在尝试将用户重定向到特定页面。 It works for a single users but i also want to redirect each users to specific page based on their login information.它适用于单个用户,但我也想根据他们的登录信息将每个用户重定向到特定页面。

How can i edit this code $user_info = get_userdata(9100008);我如何编辑此代码$user_info = get_userdata(9100008); to get current logged in users user_data ?.获取当前登录的用户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;
  }
 }

There's a function exactly for this purpose called get_current_user_id() (which is commented out in your code, for some reason).有一个专门用于此目的的函数,称为get_current_user_id() (出于某种原因,它在您的代码中被注释掉了)。

Read more in the Codex . 在 Codex 中阅读更多内容。

If you need the current user OBJECT (as your code implies), you can use wp_get_current_user() .如果您需要当前用户 OBJECT(正如您的代码所暗示的那样),您可以使用wp_get_current_user()

With

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

or或者

$data=get_currentuserinfo();

Snippet片段

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.

相关问题 如何使用WordPress SHORTINIT获取当前的登录用户 - How Can I Get the Current Logged-In User with WordPress SHORTINIT 如何在登录wordpress的用户上显示wp_users和wp_usermeta的所有信息 - how can I display all info on wp_users and wp_usermeta on user logged in wordpress 如何显示一些链接/导航来登录wordpress中的用户? - How can I show some links/navigation to logged in users in wordpress? 如何在外部php文件中获取登录用户的当前Wordpress电子邮件地址? - How can I get the current Wordpress e-mail address of a logged-in user in an external php file? 如何防止已登录的用户删除其他用户的帖子? - How can i prevent a logged in user to delete post by others users? Facebook Connect:如何将已登录用户的信息获取到PHP对象中? - Facebook Connect: How can I get a logged in user's info into a PHP object? 如何将未登录Wordpress网站的用户重定向到特殊的登录页面? - How can I redirect users who are not logged in to my Wordpress site to a special login page? Wordpress 如何使用自定义查询获取登录用户的帖子标题? - Wordpress how to use custom query to get logged in users' post title? Wordpress-如何从我关注的用户那里获得帖子 - Wordpress - How can i get the posts from the users i follow 如何获得Wordpress用户ID? - How can I get Wordpress user ID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM