登录后,会将用户角色重定向到页面

时间:2015-05-14 作者:gadss

我试图创建一个钩子,当用户登录时,它会检查用户角色。。如果用户角色为jsa_contributor 然后重定向到特定页面。但我的代码似乎不起作用。

这是代码。。

function my_login_redirect($login) {
    $data_login = get_option(\'axl_jsa_login_wid_setup\');
    $redirect_page = $data_login[0];

    $logout_redirect_page = $data_login[1];

    $current_user = get_user_by(\'login\',$login);
    if($current_user->roles == \'jsa_contributor\'){
        wp_redirect(get_permalink($redirect_page)); /* Redirect browser */
        exit();
    }
}

add_action(\'wp_login\', \'my_login_redirect\', 99);
有人对此有想法吗?提前感谢。。。

3 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

好的,那么你的第一个问题是$current_user->roles 是数组而不是字符串,因此您的条件将不为true;)

我的另一个担心是你用的钩子。一些插件可能会将其用于其他目的,杀死脚本可能是有害的。和有更好的钩子;)

第一种解决方案:使用login_redirect 挂钩

function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {

        if ( in_array( \'jsa_contributor\', $user->roles ) ) {
            // redirect them to the default place
            $data_login = get_option(\'axl_jsa_login_wid_setup\');

            return get_permalink($data_login[0]);
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}
add_filter( \'login_redirect\', \'my_login_redirect\', 10, 3 );
但您必须记住,此代码只有在登录后才会重定向用户。它不会限制用户访问wp admin。

如果这些用户不能访问wp admin,那么您应该使用另一种方法。。。

第二种解决方案:限制wp admin访问

function my_restrict_wpadmin_access() {
    if ( ! defined(\'DOING_AJAX\') || ! DOING_AJAX ) {
        $user = wp_get_current_user();

        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
            if ( in_array(\'jsa_contributor\', $user->roles) ) {
                $data_login = get_option(\'axl_jsa_login_wid_setup\');
                wp_redirect( get_permalink($data_login[0]) );
                die;
            }
        }
    }
}
add_action( \'admin_init\', \'my_restrict_wpadmin_access\' );

SO网友:NJENGAH

您也可以使用woocommerce_login_redirect如果你有WooCommerce。

apply_filters( \'woocommerce_login_redirect\',  $redirect,  $user );
您可以使用如下过滤器添加基于角色的重定向:

if( !function_exists(\'wc_custom_user_redirect\') ) {
    function wc_custom_user_redirect( $redirect, $user ) {
        // Get the first of all the roles assigned to the user
        $role = $user->roles[0];
        $dashboard = admin_url();

        if( $role == \'administrator\' ) {

            // Redirect administrators to the dashboard
            $redirect = $dashboard;
        } elseif ( $role == \'shop-manager\' ) {

            // Redirect shop managers to the dashboard
            $redirect = $dashboard;
        } elseif ( $role == \'editor\' ) {

            // Redirect editors to the dashboard
            $redirect = $dashboard;

        } elseif ( $role == \'author\' ) {

            // Redirect authors to the dashboard
            $redirect = $dashboard;

        } elseif ( $role == \'customer\' || $role == \'subscriber\' ) {

            // Redirect customers and subscribers to the "My Account" page
            $redirect = get_permalink( wc_get_page_id( \'myaccount\' ) );
        } elseif ( $role == \'jsa_contributor\' ) {

            // Redirect JSA Contributor to the " JSA Contributor Dashboard" page
            if( get_option(\'jsa_contributor_dashboard_page_id\') ) { 
                $redirect  = get_permalink( get_option(\'jsa_contributor_dashboard_page_id\') ); 
            } else {
                $redirect = home_url();
            }; 
        } elseif ( $role == \'JSA Member\' ) {

            // Redirect Jsa to the "JSA Member Dashboard" page
            if( get_option(\'jsa_member_dashboard_page_id\') ) { 
                $redirect = get_permalink( get_option(\'jsa_member_dashboard_page_id\') ); 
            } else {
                $redirect = home_url();
            };
        } else {

            // Redirect any other role to the previous visited page or, if not available, to the home
            $redirect = wp_get_referer() ? wp_get_referer() : home_url();
        }
        return $redirect;
    }
}

add_filter( \'woocommerce_login_redirect\', \'wc_custom_user_redirect\', 10, 2 );

SO网友:Mayank Dudakiya

Reference: After Login Redirection By User Roles In WordPress

/*@ After login redirection by user role */
if ( !function_exists(\'tf_after_login_redirection_by_user_roles\') ):

    add_filter( \'login_redirect\', \'tf_after_login_redirection_by_user_roles\', 10, 3 );  
    function tf_after_login_redirection_by_user_roles( $redirect_to, $request, $user ) {
        
        global $user;

        if ( isset( $user->roles ) && is_array( $user->roles ) ) :

            if ( in_array( \'student\', $user->roles ) ) :

                $page_id = 77;
                
                return get_permalink($page_id);

            elseif ( in_array( \'teacher\', $user->roles ) ) :

                $page_id = 78;
                
                return get_permalink($page_id);

            elseif ( in_array( \'super-user\', $user->roles ) ) :

                $page_id = 79;
                
                return get_permalink($page_id);

            else:
                return home_url();
            endif;
            
        else:
            return $redirect_to;
        endif;
    }
endif;
结束

相关推荐

301 Redirect Loop

我有wordpress网站托管在openshift上,比如说。com。我正在重定向示例。com到www.example。com(非www到www)。我已经为www和非www版本创建了别名,并用app name更新了我的cname。我试着将非www cname记录指向www版本,但除了主页(相同的301重定向循环)外,它仍在工作,所以我将非www cname还原回应用程序url。现在www版本正在提供服务,但非www版本正在通过重定向到自身来创建301重定向循环。我的wordpress网站url是www.e