WordPress 用户注册后自动登录/首次登录跳转到指定页面

善良是人生的正能量,是一种能够面对人生一切困苦的力量,是能够化解一切矛盾和摩擦的力量,是能够带来和平与幸福的力量,是能够让世界越来越美好的力量。

对于开放注册的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

到此这篇关于WordPress 用户注册后自动登录/首次登录跳转到指定页面就介绍到这了。最深的陪伴是你明知道自己可怜却还对他爱不释手。更多相关WordPress 用户注册后自动登录/首次登录跳转到指定页面内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress做公司官网好吗?会不会显得档次很低?

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?