首页» 教程» Wordpress教程» WordPress 首次登录跳转到指定页面-用户交互(四十)

WordPress 首次登录跳转到指定页面-用户交互(四十)

Hello,嗨,大家好,我是哈喽猿。

这里是哈喽猿网

今天推送的是wordpress教程的文章,感谢您宝贵的时间阅读

对于打开注册的WordPress站点,需要仔细考虑用户登录后的页面跳转(重定向)。在以前共享了"WordPress登录/注销(注销)并返回到以前访问过的页面后,我们今天讨论的是将首次登录或在指定时间内登录的用户重定向到指定的页面。"以下代码可以添加到主题的函数 functions.php

自动登录和重定向

 

注册后自动登录并重定向到指定的页面实际上是实现第一次登录重定向的最简单方法。

/**
  * 用户注册成功后自动登录,并跳转到指定页面
  * 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 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记录登录数,如果在一定时间内登录,且登录次数少于指定次数,则重定向到指定页面

/**
  * 注册后一定时间内首次登录重定向(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小时),且字段值小于指定次数,则重定向

/**
  * 注册后一定时间内首次登录重定向(字段 版)
  * 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 );

 

 

发表评论

0 评论

提供最优质的资源集合

站长留言