Home How to properly create multiple conditions to redirect users roles to different pages
Reply: 1

How to properly create multiple conditions to redirect users roles to different pages

Kenny Amaro
1#
Kenny Amaro Published in 2018-02-11 22:16:02Z

Basically I have two user roles that can access to my website, the first one is a subscriber and the second one a customer, and I want them to meet certain conditions:

  1. if any of this one is not logged in try to access to shop page, they are gonna be redirected to the register page
  2. if there is a subscriber or a customer logged in and they try to go to the register page, they are gonna be redirected to their "my account" page
  3. if there is a subscriber logged in but is not a customer and try to access to the shop page, he is gonna be redirected to the id confirmation page (a page with a form that subscribers need to fill and send, and then later we confirm his identity and change their role to customers)
  4. if there is a customer logged in, he can access to the shop page, and not the id confirmation page.
  5. not logged in users can't access to the id confirmation page, they are gonna be redirected to the register page

I have this code below, but it gives me an issue, don't know why it redirect my customers to "my account" page after going to the shop page:

add_action( 'template_redirect', 'subscribers_redirection' );
function subscribers_redirection() {
        if ( !is_user_logged_in() && !current_user_can( 'subscriber' ) && !current_user_can( 'customer' ) && ( is_shop() || is_product_category() || is_product_tag() || is_product() ) ) {
        wp_redirect( home_url( '/register' ) );
        } else if ( is_user_logged_in() && current_user_can( 'subscriber' ) && !current_user_can( 'customer' ) && ( is_shop() || is_product_category() || is_product_tag() || is_product() ) ){
        wp_redirect( home_url( '/id-confirmation' ) );
        } else if ( is_user_logged_in() && current_user_can( 'subscriber' ) && current_user_can( 'customer' ) && ( is_shop() || is_product_category() || is_product_tag() || is_product() ) ){
        wp_redirect( home_url( '/shop' ) );
        } else if ( is_user_logged_in() && is_page( 'Register' ) ){
        wp_redirect( home_url( '/my-account' ) );
        } else if ( !current_user_can( 'subscriber' ) && is_page( 'id-confirmation' ) ){
        wp_redirect( home_url( '/register' ) );
        exit(); 
        }
}

My assumption:

I think that for some reason my customers are been redirected to the register page first, but one of the conditions says that if you are a customer and are on the register page, you are gonna be redirected to "my account" page, the thing is why they are been redirected to register first after going to shop.

also I tested with this other code below too:

add_action( 'template_redirect', 'verify_logged_in' );
function verify_logged_in() {

        if ( !is_user_logged_in() ) {
                if ( is_shop() || is_product_category() || is_product_tag() || is_product() || is_page( 'id-confirmation' ) ) {
                        wp_redirect( home_url( '/register' ) );        
                } else ( is_user_logged_in() ) {
                        function subscribers_redirection() {
                                $current_user = wp_get_current_user();
                                $user_id = $current_user->ID;
                                $user_info = get_userdata($user_id);
                                $user_rols = implode(', ', $user_info->roles) . "\n";
                                switch ($user_rols) {
                                    case "subscriber":
                                        if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) {
                                                wp_redirect( home_url( '/id-confirmation' ) );        
                                        } else if ( is_page( 'Register' ) ) {
                                                wp_redirect( home_url( '/my-account' ) );       
                                        } else {
                                                exit();
                                        }
                                        break;
                                    case "customer":
                                        if ( is_page( 'id-confirmation' ) || is_page( 'Register' )) {
                                                wp_redirect( home_url( '/my-account' ) );        
                                        } else {
                                                exit();
                                        }
                                        break;
                                    default:
                                       exit();
                                }        
                        }
                        add_action( 'template_redirect', 'subscribers_redirection' );
                }
        }
}

but didn't work the second if statement "if is logged in". Different from the first code, I have not assumptions on why is not working this second one.

Artem Anokhin
2#
Artem Anokhin Reply to 2018-02-11 22:37:24Z

May be you need this way of your script? As for me, this algorithm is more unterstandable. If wp_redirect works correctly on your web-site, then this filter will work as you expected. Firstly you detect the page of visit and after that you check the login status. (true_case) ? something_one : something_two is the same as if (true) { something_one } else { something_two }.

if (is_shop() || is_product_category() || 
    is_product_tag() || is_product() || is_page( 'id-confirmation' )) {

    (is_user_logged_in()) ? subscribers_redirection() : wp_redirect(home_url('/register'));
}

More over, you have too many duplicates of code. Try to simplify it, because half of it isn't needed for such easy task.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309067 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO