Title: Restrict Access with Post Code
Last modified: January 27, 2020

---

# Restrict Access with Post Code

 *  Resolved [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/)
 * Is it possible to restrict access to certain categories using their profile post
   codes?

Viewing 15 replies - 16 through 30 (of 31 total)

[←](https://wordpress.org/support/topic/restrict-access-with-post-code/?output_format=md)
[1](https://wordpress.org/support/topic/restrict-access-with-post-code/?output_format=md)
2 [3](https://wordpress.org/support/topic/restrict-access-with-post-code/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/restrict-access-with-post-code/page/3/?output_format=md)

 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384631)
 * Thank you [@crslz](https://wordpress.org/support/users/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](https://wordpress.org/support/users/thoseitguys/).
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384662)
 * 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
       }
       ```
   
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384680)
 * Thank you [@crslz](https://wordpress.org/support/users/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.
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384683)
 *     ```
       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');
       ```
   
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384687)
 * Thank you so much. [@crslz](https://wordpress.org/support/users/crslz/)
 * This also gives my site a wordpress error?
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384693)
 * It might be useful to mention which error? I just tested this and it works here
   without error messages.
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384695)
 * sorry about that: [@crslz](https://wordpress.org/support/users/crslz/)
 * It says There has been a critical error on your website.
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384700)
 * Nevermind [@crslz](https://wordpress.org/support/users/crslz/) it was my fault.
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384703)
 * Thank you so much for the help. Is there anywhere I can +rep you or something
   [@crslz](https://wordpress.org/support/users/crslz/)
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384708)
 * that does not say much of course, if you want to further adjust the code it can
   be useful to go through [this](https://wordpress.org/support/article/debugging-in-wordpress/).
   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
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384710)
 * [@crslz](https://wordpress.org/support/users/crslz/) il try figure out how to
   make them hidden myself or show a pop up message stating sorry you cannot access
   that category 🙂
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384721)
 * [@crslz](https://wordpress.org/support/users/crslz/) Thank you for the help i
   mistyped one line of your code. Its all working sir!
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384726)
 * [@thoseitguys](https://wordpress.org/support/users/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
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384730)
 * [@crslz](https://wordpress.org/support/users/crslz/) no problem! Now to fiddle
   with
 * alert(“Your cant access this category”) to pop up if they click it 😀
 * But this I will figure out 🙂
 * Thank you again [@crslz](https://wordpress.org/support/users/crslz/)
 *  Thread Starter [thoseitguys](https://wordpress.org/support/users/thoseitguys/)
 * (@thoseitguys)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/#post-12384796)
 * oh [@crslz](https://wordpress.org/support/users/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?

Viewing 15 replies - 16 through 30 (of 31 total)

[←](https://wordpress.org/support/topic/restrict-access-with-post-code/?output_format=md)
[1](https://wordpress.org/support/topic/restrict-access-with-post-code/?output_format=md)
2 [3](https://wordpress.org/support/topic/restrict-access-with-post-code/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/restrict-access-with-post-code/page/3/?output_format=md)

The topic ‘Restrict Access with Post Code’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 31 replies
 * 2 participants
 * Last reply from: [crslz](https://wordpress.org/support/users/crslz/)
 * Last activity: [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/page/3/#post-12385737)
 * Status: resolved