[英]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 usersuser_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.