2015-09-04 90 views
-1

我想重定向用户到特定页面。它适用于单个用户,但我也希望根据用户的登录信息将每个用户重定向到特定页面。我如何获得用户在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; 
    } 
} 

回答

0

随着

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; 
} 
+0

感谢传单,我这样说,但带我到主页'global $ current_user; $ id = $ current_user-> ID; $ user_info = get_userdata($ id);如果($ user_id == 0){ $ redirect_to ='http://example.com/'.$user_info->user_login.'/'; 返回$ redirect_to; } }' – Kinfe

+0

这是因为您将id变为$ id,但请检查$ user_id。我用完整的代码片段更新了答案。 –

+0

当登录时,我得到空白屏幕时,我刷新它需要我到我的帐户页我改变ID为'$ id == 9100010' – Kinfe

1

有一个功能正是为了这个目的称为get_current_user_id()(这是在你的代码注释掉,出于某种原因)。

阅读全文in the Codex

如果您需要当前用户OBJECT(如您的代码所暗示),则可以使用wp_get_current_user()

+1

看起来像他希望的用户对象,而不是ID。他应该使用[https://codex.wordpress.org/Function_Reference/wp_get_current_user](https://codex.wordpress.org/Function_Reference/wp_get_current_user),这样他就不必调整任何代码,除了get_userdata ()' – Ohgodwhy

+1

@Ohgodwhy ...问题标题专门为用户标识提出要求...但是再次,他/她的代码似乎在后面反驳。 – rnevius

+0

@Ohgodwhy谢谢当我替换'$ user_info = get_userdata(9100008);'到'$ user_info = wp_get_current_user();'它将我重定向到主页 – Kinfe

相关问题