繁体   English   中英

登录到WordPress中的特定页面后如何重定向?

[英]How to redirect after login to a particular page in WordPress?

我想检查当前用户是否有权阅读它。 然后打开特殊页面。

换句话说,我想在登录到WordPress中的特定页面后重定向,并限制其他用户访问它。

用户特定内容是一个很棒的插件,可让您顺利地通过角色或用户名指定个人或用户组,并授予他们访问特定内容的权限。

更新 :您可以使用Peter的Login Redirect为特定用户,具有特定角色的用户,具有特定功能的用户以及所有其他用户的覆盖规则定义一组重定向规则。 另外,设置用于后注册的重定向URL。

根据本文

在WordPress中首次登录时重定向用户

在基于成员资格的WordPress网站和其他要向新用户显示特殊欢迎消息或说明的网站上,可以实现一些自定义登录重定向功能。 每个用户只能启动一次此功能(或对于前几次登录而言)。

在代码方面,实现此功能的重要元素是使用内置的WordPress“ login_redirect”过滤器,并存储有关用户是否获得“首次登录”待遇的信息。 有两种方法可以将信息存储在cookie或用户的元信息中(存储在WordPress数据库的“ wp_usermeta”表中)。

这是一些示例代码,您可以在主题的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 );

在第一个登录插件上下载基于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;
    // 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 );

在第一个登录插件上下载基于用户元数据的重定向

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM