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:
- if any of this one is not logged in try to access to shop page, they are gonna be redirected to the register page
- 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
- 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)
- if there is a customer logged in, he can access to the shop page, and not the id confirmation page.
- 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:
- if any of this one is not logged in try to access to shop page, they are gonna be redirected to the register page
- 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
- 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)
- if there is a customer logged in, he can access to the shop page, and not the id confirmation page.
- 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 badges1 Answer
Reset to default 0couple 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;
}
}