最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览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.

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.

Share Improve this question asked Feb 11, 2018 at 20:31 Kenny AmaroKenny Amaro 1134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论