如何在登录到WordPress的特定页面后重定向?
用户特定的内容是一个伟大的插件,顺利允许您通过角色或用户名指定的个人或用户组,并让他们访问特定内容。
更新: 您可以使用彼得登录重定向定义一组针对特定用户,用户与特定的角色,用户具有特定的功能重定向规则,以及所有其他用户的毯子规则。另外,请为注册后设置重定向网址。
重定向在第一次登录的用户在WordPress
在会员制的WordPress网站和要显示一个特殊的欢迎信息或新的用户指令的其他网站,你可以实现一些自定义登录重定向功能。该功能只会为每个用户启动一次(或前几次登录)。
这些功能的代码端的重要元素是使用内置的WordPress“login_redirect”过滤器,并存储有关用户是否已获得“第一次登录”处理的信息。有几种可能的方法来存储信息,无论是在cookie中还是在用户的元信息中(存储在“wp_usermeta”表中的WordPress数据库中)。
下面是一些示例代码,您可以在主题functions.php文件或插件使用方法:
基于Cookie的解决方案
// Send new users to a special page
function redirectOnFirstLogin($redirect_to, $requested_redirect_to, $user)
{
// URL to redirect to
$redirect_url = 'http://yoursite.com/firstloginpage';
// How many times to redirect the user
$num_redirects = 1;
// Cookie-based solution: captures users who registered within the last n hours
// The reason to set it as "last n hours" is so that if a user clears their cookies or logs in with a different browser,
// they don't get this same redirect treatment long after they're already a registered user
// 172800 seconds = 48 hours
$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_' . $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);
Download the cookie-based redirect on first login plugin
基于用户元表的解决方案
// Send new users to a special page
function redirectOnFirstLogin($redirect_to, $requested_redirect_to, $user)
{
// URL to redirect to
$redirect_url = 'http://yoursite.com/firstloginpage';
// How many times to redirect the user
$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 seconds = 48 hours
$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';
// Third parameter ensures that the result is a string
$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://wploop.com/specify-users-name-role-show-exclusive-content/ – EniGma
检查此https://*.com/questions/4267285/redirect-user-after-first-login-in-wordpress?rq=1 – EniGma