0

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.

0

couple notes!

No need to check for login status if you are checking capabilities. A visitor (not logged in) only has the exists capability, nothing more.

Also, wp_redirect() should always be followed by exit; in most cases. In your example only the last one was followed by exit;. Otherwise this could create loops.

I've refactored your code a bit, I think this should work:

add_action( 'template_redirect', 'subscribers_redirection' );
function subscribers_redirection() {
    $is_product_page = ( is_shop() || is_product_category() || is_product_tag() || is_product() );
    $is_logged_in    = is_user_logged_in();
    $is_subscriber   = current_user_can( 'subscriber' );
    $is_customer     = current_user_can( 'customer' );

    $redirect = false;
    if ( ! $is_logged_in && $is_product_page ) { // No need to check for user roles if the user isn't logged in.
        $redirect = home_url( '/register' );
    } else if ( $is_subscriber && ! $is_customer && $is_product_page ) {
        $redirect = home_url( '/id-confirmation' );
    } else if ( $is_customer && $is_product_page ) { // No need to check for a subscriber if the user already is a customer.
        $redirect = home_url( '/shop' );
    } else if ( $is_logged_in && is_page( 'register' ) ) {
        $redirect = home_url( '/my-account' );
    } else if ( ! $is_subscriber && is_page( 'id-confirmation' ) ) {
        $redirect = home_url( '/register' );
    }

    if ( $redirect ) {
        wp_redirect( $redirect );
        exit;
    }
}
  • is not working :S – Kenny Amaro Feb 12 '18 at 16:08
  • What part isn't working? Did you try some debugging as to what is happening and is returning true/false when it shouldn't? – Jory Hogeveen Feb 28 '18 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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