2010-11-24 76 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