2010-11-24 81 views
6

此代码检查用户是否第一次登录,即注册后。如果是,我想将他重定向到一个自定义页面。否则,将他重定向到主页或管理页面。首次在WordPress登录后重定向用户?

function mylogin_redirect() { 
    global $user_ID; 
    if($user_ID) { 
     $user_info = get_userdata($user_ID); 
     // If user_registered date/time is less than 48hrs from now 
     // Message will show for 48hrs after registration 
     if (strtotime($user_info->user_registered) > (time() - 172800)) { 
      header("Location: http://example.com/custompage"); 
     } elseif(current_user_can('manage_options')) { 
      header("Location: http://example.com/wp-admin/"); 
     } else { 
      header("Location: http://example.com/"); 
     } 
    } 
} 
add_action('wp_head', 'mylogin_redirect'); 

但它不起作用?我的猜测是它不会上瘾成wp_head ...... 我尝试以下使用login_redirect过滤器:

function mylogin_redirect($redirect_to, $url_redirect_to = '', $user = null) { 
    global $user_ID; 
    if($user_ID) { 
     $user_info = get_userdata($user_ID); 
     // If user_registered date/time is less than 48hrs from now 
     // Message will show for 48hrs after registration 
     if (strtotime($user_info->user_registered) > (time() - 172800)) { 
      return get_bloginfo('url') . "/custompage/"; 
     } elseif(current_user_can('manage_options')) { 
      return admin_url(); 
     } else { 
      return get_bloginfo('url'); 
     } 
    } 
} 
add_filter('login_redirect', 'mylogin_redirect'); 

虽然它记录了我,它不会让我在任何地方,但到http://example.com/wp-login.php用,而不是一个空白页。

更新: 好的,我不知道发生了什么事。使用过滤器挂钩,我可以在第二次登录后才能到达预期的目的地。那么不是真正的第二次登录,而是第二次点击登录按钮。我这样做:输入凭证 - >登录 - >(错误的页面) - >回击按钮 - >再次输入凭据 - >登录 - >(正确的页面)。奇怪的。

回答

4

您需要像这样调整过滤器调用;

// filter name, callback, priority, accepted args 
add_filter('login_redirect', 'mylogin_redirect', 10, 3); 
+0

试了一下,没有工作。它仍然将我重定向到../wp-login.php而没有表单。过滤器的问题是解析器没有进入`if`块。因为如果我删除`if`块并将其替换为`return admin_url()`语句,它就可以正常工作。 – Joann 2010-11-24 14:03:39