本文目錄1自動登錄並重定向2一定時間內登錄重定向3一定時間內首次登錄重定向(Cookie 版)4一定時間內首次登錄重定向(字段 版)
對於開放註冊的WordPress站點,用戶登錄後的頁面跳轉(重定向)是需要好好考慮的。之前分享過《WordPress 登錄/登出(註銷)後返回之前訪問的頁面》,今天要說的是將首次登錄或者在指定時間內登錄的用戶,重定向到指定頁面。以下代碼都可以添加到主題的 functions.php
自動登錄並重定向
註冊後自動登錄並且重定向到指定頁面,其實也是實現首次登錄重定向的最簡便的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * 用戶註冊成功後自動登錄,並跳轉到指定頁面 * https://www.wpdaxue.com/user-first-login-redirect.html */ function auto_login_new_user( $user_id ) { // 用戶註冊後自動登錄 wp_set_current_user($user_id); wp_set_auth_cookie($user_id); // 這裡跳轉到 http://域名/about 頁面,請根據自己的需要修改 wp_redirect( home_url().'about' ); exit; } add_action( 'user_register', 'auto_login_new_user'); |
/**
* 用戶註冊成功後自動登錄,並跳轉到指定頁面
* https://www.wpdaxue.com/user-first-login-redirect.html
*/
function auto_login_new_user( $user_id ) {
// 用戶註冊後自動登錄
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
// 這裡跳轉到 http://域名/about 頁面,請根據自己的需要修改
wp_redirect( home_url().’about’ );
exit;
}
add_action( ‘user_register’, ‘auto_login_new_user’);
一定時間內登錄重定向
註冊後的一定時間內,不管是第幾次登錄,都跳轉到指定頁面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * 註冊一定時間內登錄重定向到指定頁面 * https://www.wpdaxue.com/user-first-login-redirect.html */ function time_limit_login_redirect( $to, $requested, $user ){ if( !isset( $user->user_login ) ){ return $to; } $regtime = strtotime($user->user_registered); $now = strtotime("now"); $diff = $now - $regtime; $hours = $diff / 60 / 60; if( $hours < 48 ){ // 註冊後48小時內登錄重定向到該頁面 return "/about"; } else { return admin_url(); //WP管理後臺 } } add_filter('login_redirect', 'time_limit_login_redirect', 10, 3); |
/**
* 註冊一定時間內登錄重定向到指定頁面
* https://www.wpdaxue.com/user-first-login-redirect.html
*/
function time_limit_login_redirect( $to, $requested, $user ){
if( !isset( $user->user_login ) ){
return $to;
}
$regtime = strtotime($user->user_registered);
$now = strtotime("now");
$diff = $now – $regtime;
$hours = $diff / 60 / 60;
if( $hours < 48 ){ // 註冊後48小時內登錄重定向到該頁面
return "/about";
} else {
return admin_url(); //WP管理後臺
}
}
add_filter(‘login_redirect’, ‘time_limit_login_redirect’, 10, 3);
一定時間內首次登錄重定向(Cookie 版)
使用Cookie記錄登錄次數,如果在一定時間內登錄,且登錄次數小於指定次數,就重定向到指定頁面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/** * 註冊後一定時間內首次登錄重定向(Cookie 版) * https://www.wpdaxue.com/user-first-login-redirect.html */ function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user ) { // 重定向的頁面地址 $redirect_url = 'http://yoursite.com/firstloginpage'; // 重定向的次數 $num_redirects = 1; // 通過 Cookie 記錄登錄次數 // 指定註冊後多久時間內登錄(防止用戶清空 Cookie 後重復重定向) // 172800 秒 = 48 小時 $message_period = 172800; // If they're on the login page, don't do anything if( !isset( $user->user_login ) ) { return $redirect_to; } // 用來保存登錄記錄的 Cookie 鍵名 $key_name = 'redirect_on_first_login_' . $user->ID; if( strtotime( $user->user_registered ) > ( time() - $message_period ) && ( !isset( $_COOKIE[$key_name] ) || intval( $_COOKIE[$key_name] ) < $num_redirects ) ) { if( isset( $_COOKIE[$key_name] ) ) { $num_redirects = intval( $_COOKIE[$key_name] ) + 1; } setcookie( $key_name, $num_redirects, time() + $message_period, COOKIEPATH, COOKIE_DOMAIN ); return $redirect_url; } else { return $redirect_to; } } add_filter( 'login_redirect', 'redirectOnFirstLogin', 10, 3 ); |
/**
* 註冊後一定時間內首次登錄重定向(Cookie 版)
* https://www.wpdaxue.com/user-first-login-redirect.html
*/
function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user )
{
// 重定向的頁面地址
$redirect_url = ‘http://yoursite.com/firstloginpage’;
// 重定向的次數
$num_redirects = 1;
// 通過 Cookie 記錄登錄次數
// 指定註冊後多久時間內登錄(防止用戶清空 Cookie 後重復重定向)
// 172800 秒 = 48 小時
$message_period = 172800; // If they’re on the login page, don’t do anything
if( !isset( $user->user_login ) )
{
return $redirect_to;
}
// 用來保存登錄記錄的 Cookie 鍵名
$key_name = ‘redirect_on_first_login_’ . $user->ID; if( strtotime( $user->user_registered ) > ( time() – $message_period )
&& ( !isset( $_COOKIE[$key_name] ) || intval( $_COOKIE[$key_name] ) < $num_redirects )
)
{
if( isset( $_COOKIE[$key_name] ) )
{
$num_redirects = intval( $_COOKIE[$key_name] ) + 1;
}
setcookie( $key_name, $num_redirects, time() + $message_period, COOKIEPATH, COOKIE_DOMAIN );
return $redirect_url;
}
else
{
return $redirect_to;
}
}
add_filter( ‘login_redirect’, ‘redirectOnFirstLogin’, 10, 3 );
一定時間內首次登錄重定向(字段 版)
使用自定義字段來保存登錄次數,如果用戶註冊時間小於指定時間(比如 48小時),且字段的數值小於指定次數,就重定向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/** * 註冊後一定時間內首次登錄重定向(字段 版) * https://www.wpdaxue.com/user-first-login-redirect.html */ function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user ) { // 重定向的頁面 $redirect_url = 'http://yoursite.com/firstloginpage'; // 重定向的次數 $num_redirects = 1; // If implementing this on an existing site, this is here so that existing users don't suddenly get the "first login" treatment // On a new site, you might remove this setting and the associated check // Alternative approach: run a script to assign the "already redirected" property to all existing users // Alternative approach: use a date-based check so that all registered users before a certain date are ignored // 172800 秒 = 48 小時(用來排除那些老用戶) $message_period = 172800; // If they're on the login page, don't do anything if( !isset( $user->user_login ) ) { return $redirect_to; } //添加一個字段來記錄登錄次數 $key_name = 'redirect_on_first_login'; $current_redirect_value = get_user_meta( $user->ID, $key_name, true ); if( strtotime( $user->user_registered ) > ( time() - $message_period ) && ( '' == $current_redirect_value || intval( $current_redirect_value ) < $num_redirects ) ) { if( '' != $current_redirect_value ) { $num_redirects = intval( $current_redirect_value ) + 1; } update_user_meta( $user->ID, $key_name, $num_redirects ); return $redirect_url; } else { return $redirect_to; } } add_filter( 'login_redirect', 'redirectOnFirstLogin', 10, 3 ); |
/**
* 註冊後一定時間內首次登錄重定向(字段 版)
* https://www.wpdaxue.com/user-first-login-redirect.html
*/
function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user )
{
// 重定向的頁面
$redirect_url = ‘http://yoursite.com/firstloginpage’;
// 重定向的次數
$num_redirects = 1;
// If implementing this on an existing site, this is here so that existing users don’t suddenly get the "first login" treatment
// On a new site, you might remove this setting and the associated check
// Alternative approach: run a script to assign the "already redirected" property to all existing users
// Alternative approach: use a date-based check so that all registered users before a certain date are ignored
// 172800 秒 = 48 小時(用來排除那些老用戶)
$message_period = 172800; // If they’re on the login page, don’t do anything
if( !isset( $user->user_login ) )
{
return $redirect_to;
}
//添加一個字段來記錄登錄次數
$key_name = ‘redirect_on_first_login’;
$current_redirect_value = get_user_meta( $user->ID, $key_name, true );
if( strtotime( $user->user_registered ) > ( time() – $message_period )
&& ( ” == $current_redirect_value || intval( $current_redirect_value ) < $num_redirects )
)
{
if( ” != $current_redirect_value )
{
$num_redirects = intval( $current_redirect_value ) + 1;
}
update_user_meta( $user->ID, $key_name, $num_redirects );
return $redirect_url;
}
else
{
return $redirect_to;
}
}
add_filter( ‘login_redirect’, ‘redirectOnFirstLogin’, 10, 3 );
參考資料:
http://wordpress.stackexchange.com/questions/24164/
http://www.theblog.ca/wordpress-redirect-first-login