對於已經開放註冊的 WordPress 站點,用户登錄後的頁面跳轉是必須要考慮的。今天就説説 WordPress 登錄後跳轉或指定頁面,以下代碼都可以添加到主題的 functions.php

WordPress 登錄後跳轉指定頁面

  1. /**
  2. * 用户註冊成功後自動登錄,並跳轉到指定頁面
  3. */
  4. function auto_login_new_user( $user_id ){
  5.         // 用户註冊後自動登錄
  6.         wp_set_current_user($user_id);
  7.         wp_set_auth_cookie($user_id);
  8.         // 這裏跳轉到 http://域名/about 頁面,請根據自己的需要修改
  9.         wp_redirect( home_url().'about');
  10.         exit;
  11. }
  12. add_action('user_register','auto_login_new_user');

一定時間內登錄重定向

  1. /**
  2. * 註冊一定時間內登錄重定向到指定頁面
  3. */
  4. function time_limit_login_redirect( $to, $requested, $user ){
  5.         if(!isset( $user->user_login )){
  6.                 return $to;
  7.         }
  8.         $regtime = strtotime($user->user_registered);
  9.         $now = strtotime("now");
  10.         $diff = $now - $regtime;
  11.         $hours = $diff /60/60;
  12.         if( $hours <48){// 註冊後 48 小時內登錄重定向到該頁面
  13.                 return"/about";
  14.         }else{
  15.                 return admin_url();//WP 管理後台
  16.         }
  17. }
  18. add_filter('login_redirect','time_limit_login_redirect',10,3);

註冊後的一定時間內,不管是第幾次登錄,都跳轉到指定頁面。

一定時間內首次登錄重定向 (Cookie 版)

  1. /**
  2. * 註冊後一定時間內首次登錄重定向 (Cookie 版)
  3. */
  4. function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user )
  5. {
  6. // 重定向的頁面地址
  7.         $redirect_url ='http://yoursite.com/firstloginpage';
  8. // 重定向的次數
  9.         $num_redirects =1;
  10. // 通過 Cookie 記錄登錄次數
  11. // 指定註冊後多久時間內登錄 (防止用户清空 Cookie 後重復重定向)
  12. // 172800 秒 = 48 小時
  13.         $message_period =172800;
  14. // If they're on the login page, don't do anything
  15.         if(!isset( $user->user_login ))
  16.         {
  17.                 return $redirect_to;
  18.         }
  19. // 用來保存登錄記錄的 Cookie 鍵名
  20.         $key_name ='redirect_on_first_login_'. $user->ID;
  21.         if( strtotime( $user->user_registered )>( time()- $message_period )
  22.                 &&(!isset( $_COOKIE[$key_name])|| intval( $_COOKIE[$key_name])< $num_redirects )
  23.                 )
  24.         {
  25.                 if( isset( $_COOKIE[$key_name]))
  26.                 {
  27.                         $num_redirects = intval( $_COOKIE[$key_name])+1;
  28.                 }
  29.                 setcookie( $key_name, $num_redirects, time()+ $message_period, COOKIEPATH, COOKIE_DOMAIN );
  30.                 return $redirect_url;
  31.         }
  32.         else
  33.         {
  34.                 return $redirect_to;
  35.         }
  36. }
  37. add_filter('login_redirect','redirectOnFirstLogin',10,3);

使用 Cookie 記錄登錄次數,如果在一定時間內登錄,且登錄次數小於指定次數,就重定向到指定頁面。

一定時間內首次登錄重定向 (字段 版)

  1. /**
  2. * 註冊後一定時間內首次登錄重定向 (字段 版)
  3. */
  4. function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user )
  5. {
  6. // 重定向的頁面
  7.         $redirect_url ='http://yoursite.com/firstloginpage';
  8. // 重定向的次數
  9.         $num_redirects =1;
  10. // If implementing this on an existing site, this is here so that existing users don't suddenly get the "first login" treatment
  11. // On a new site, you might remove this setting and the associated check
  12. // Alternative approach: run a script to assign the "already redirected" property to all existing users
  13. // Alternative approach: use a date-based check so that all registered users before a certain date are ignored
  14. // 172800 秒 = 48 小時 (用來排除那些老用户)
  15.         $message_period =172800;
  16. // If they're on the login page, don't do anything
  17.         if(!isset( $user->user_login ))
  18.         {
  19.                 return $redirect_to;
  20.         }
  21. //添加一個字段來記錄登錄次數
  22.         $key_name ='redirect_on_first_login';
  23.         $current_redirect_value = get_user_meta( $user->ID, $key_name,true);
  24.         if( strtotime( $user->user_registered )>( time()- $message_period )
  25.                 &&(''== $current_redirect_value || intval( $current_redirect_value )< $num_redirects )
  26.                 )
  27.         {
  28.                 if(''!= $current_redirect_value )
  29.                 {
  30.                         $num_redirects = intval( $current_redirect_value )+1;
  31.                 }
  32.                 update_user_meta( $user->ID, $key_name, $num_redirects );
  33.                 return $redirect_url;
  34.         }
  35.         else
  36.         {
  37.                 return $redirect_to;
  38.         }
  39. }
  40. add_filter('login_redirect','redirectOnFirstLogin',10,3);

使用自定義字段來保存登錄次數,如果用户註冊時間小於指定時間 (比如 48 小時),且字段的數值小於指定次數,就重定向!