Restrict Access with Post Code
-
Is it possible to restrict access to certain categories using their profile post codes?
-
Thank you @crslz that works perfectly!
am I right that if i write the code like this:
if( $category_id == 3076,2931,1231 )
it will block all those categories?
-
This reply was modified 6 years, 4 months ago by
thoseitguys.
Hi,
If you want to check for multiple id’s you can add them via an array (same principle as with the post codes)
$allowed_category_id = array(3076, 2931, 1231); if ( in_array($category_id, $allowed_category_id) ) { // do something }Thank you @crslz
so something like this?
function my_restrict_access() {
if ( is_product_category() ) {
$category = get_queried_object();if ( $category ) {
$category_id = $category->term_id;if($allowed_category_id = array(3076, 3179); {
if ( !is_user_logged_in() ) {
wp_redirect( ‘/’ );
exit;
} else {
global $woocommerce;
$customer = new WC_Customer();
$customer_postcode = $woocommerce->customer->get_billing_postcode();// uncomment line below for debug purposes, this will show you the postcode on the current page
//echo $customer_postcode;$allowed_post_code = array(3610, 3626, 3650);
if ( !in_array($customer_postcode, $allowed_post_code) ) {
wp_redirect( ‘/’ );
exit;
}
}
}
}
}
}
add_action( ‘template_redirect’, ‘my_restrict_access’);I tried it and it gave my site an error.
function my_restrict_access() { if ( is_product_category() ) { $category = get_queried_object(); if ( $category ) { $category_id = $category->term_id; $allowed_category_id = array(3076, 2931, 1231); if ( in_array($category_id, $allowed_category_id) ) { if ( !is_user_logged_in() ) { wp_redirect( '/' ); exit; } else { global $woocommerce; $customer = new WC_Customer(); $customer_postcode = $woocommerce->customer->get_billing_postcode(); // uncomment line below for debug purposes, this will show you the postcode on the current page //echo $customer_postcode; $allowed_post_code = array(3610, 3626, 3650); if ( !in_array($customer_postcode, $allowed_post_code) ) { wp_redirect( '/' ); exit; } } } } } } add_action( 'template_redirect', 'my_restrict_access');Thank you so much. @crslz
This also gives my site a wordpress error?
It might be useful to mention which error? I just tested this and it works here without error messages.
sorry about that: @crslz
It says There has been a critical error on your website.
Nevermind @crslz it was my fault.
Thank you so much for the help. Is there anywhere I can +rep you or something @crslz
that does not say much of course, if you want to further adjust the code it can be useful to go through this. Here the piece of code works without error messages.
Otherwise you will have to rebuild the code step by step, as I explained in earlier posts. (step 1 .. step 2 …), this way you can see at what time things go wrong.
Regards
@crslz il try figure out how to make them hidden myself or show a pop up message stating sorry you cannot access that category 🙂
@crslz Thank you for the help i mistyped one line of your code. Its all working sir!
@thoseitguys ok, problem solved! no, this is not possible here (rep+). There is such a system on stack overflow.
A thank you so much will suffice here, 😀 regards
oh @crslz one last thing if you could. I dont know if you saw my message earlier.
Is there a way to modify this code you helped me with to just hide those Category IDs using the postal code?
-
This reply was modified 6 years, 4 months ago by
The topic ‘Restrict Access with Post Code’ is closed to new replies.