2

I am trying to create a hook that when a user login, it check the user role.. if the user role is jsa_contributor then it redirect into specific page. but it seems my code didn't work.

This is the code..

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);

does anyone have an idea about this? thanks in advance...

1

3 Answers 3

13

OK, so your first problem is that $current_user->roles is an array and not a string, so your condition won't be true ;)

My another concern would be the hook you're using. Some plugins may use it for other purposes, and killing the script can be harmful. And... there is much better hook for this ;)

First solution: using login_redirect hook

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 );

But you have to remember, that this code would redirect users only after loging in. It won't restrict users to access wp-admin.

If these users shouldn't be able to get to wp-admin, then you should use another method...

Second solution: restricting wp-admin access

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' );
3
  • this is maybe the picked answer, but its a security problem... a user can still reach other pages after he/she is logged in.
    – Sagive
    Jul 20, 2019 at 20:00
  • @SagiveSEO have you read all the answer? Especially the second part of it, where I write exactly about that problem and the second function (using admin_init) makes sure that unauthorized users won’t be able to access any backend parts? Because no - if you’ll use all the answer, such problem does not exist. Jul 21, 2019 at 11:21
  • Ohh.. right u are. apologies - missed that
    – Sagive
    Jul 21, 2019 at 11:49
0

You can alternatively use the woocommerce_login_redirectif you have WooCommerce.

apply_filters( 'woocommerce_login_redirect',  $redirect,  $user );

You can add redirect based on roles using a filter like this :

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 );
0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.