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 - 1 through 15 (of 31 total)

1 [2](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/?output_format=md)
[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/2/?output_format=md)

 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12370240)
 * Yes you can. I also assume that you want to restrict access for users who are
   not logged in (guests) whose zip code is unknown for those pages?
 *     ```
       function my_restrict_access() {
           $category = get_queried_object();
   
           if ( $category ) {
               $category_id = isset( $category->term_id );
   
               // uncomment line below for debug purposes, this will show you the category id on the current category page
               // echo $category_id;
   
               if( $category_id == 15 ) {
                   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;
   
       		if ( $customer_postcode == 9000 ) {
       		    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/#post-12371861)
 * Hi there [@crslz](https://wordpress.org/support/users/crslz/) thank you so much
   for the reply.
 * Im looking to block ALL postal codes from a certain woocommerce category:
 * Except these: 3610, 3626, 3650
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12372273)
 * Hi,
 * When validating on the post code, you can add an array with allowed post codes,
   and then adjust the validation accordingly
 * [https://www.php.net/manual/en/function.in-array.php](https://www.php.net/manual/en/function.in-array.php)
 *     ```
       function my_restrict_access() {
           $category = get_queried_object();
   
           if ( $category ) {
               $category_id = isset( $category->term_id );
   
               // uncomment line below for debug purposes, this will show you the category id on the current category page
               // echo $category_id;
   
               if( $category_id == 15 ) {
                   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');
       ```
   
 * 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/#post-12376084)
 * Thank you again [@crslz](https://wordpress.org/support/users/crslz/) I did try
   the code above ( entered my category ID and redirect url)
 * But it doesnt seem to work.
 * I assume this needs to be added to my funcitons file? Which I have tried.
 * Once again thank you for the assistance.
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12376182)
 * 1) copy/paste my code to functions.php
    2) uncomment the following rules
 * > //echo $category_id;
 *  &
 * > // echo $customer_postcode;
 * 3) post the result of the output here (sreenshot) or link to one of your category
   pages
 *  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/#post-12376210)
 * Hi there I have added the code
 * Please go to
 * [http://www.hillcrestkwikspar.co.za/online-store](http://www.hillcrestkwikspar.co.za/online-store)
 * on the left sidebar at the bottom is the X Fresh Category that i wish to restrict
   to those 3 postal codes only.
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12376304)
 * okay, and what’s the category id of that page? I don’t see the output printed
   on that page anywhere?
 *  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/#post-12377365)
 * I also noticed that. I am not sure why it has no output. The Category ID is 3078
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12377924)
 * Can you try the following steps?
 * **Step 1)** Copy/paste this code, execute and view the page
 *     ```
       function my_restrict_access() {
           echo 'step 1 works!';
       }
       add_action( 'template_redirect', 'my_restrict_access');
       ```
   
 * **Step 2)** If step 1 works, replace the code with the one from step 2
 *     ```
       function my_restrict_access() {
           $category = get_queried_object();
   
           if ( $category ) {
               echo 'step 2 works!';
           } else {
               echo 'step 2 NOT works!';
           }
       }
       add_action( 'template_redirect', 'my_restrict_access');
       ```
   
 * **Step 3)** If step 2 works, perform step 3… replace code with the one from step
   3
 *     ```
       function my_restrict_access() {
           $category = get_queried_object();
   
           if ( $category ) {
               $category_id = isset( $category->term_id );
               if( $category_id == 3078 ) {
                   echo 'step 3 works!';
               } else {
                   echo 'step 3 NOT works!';
               }
           }
       }
       add_action( 'template_redirect', 'my_restrict_access');
       ```
   
 * Up to which step does the code work without problems?
 *  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/#post-12380419)
 * Hi [@crslz](https://wordpress.org/support/users/crslz/) there sorry for the delay.
 * Step 1 Works
 * Step 2 Works
 * its at step 3 where it says not working.
 * I did confirm that the category ID is 3078.
 * Hope this helps.
 * All I want it to do is not open the category and pop a message to the user.
    -  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/#post-12380675)
 * Okay, good job! something goes wrong with the category id, can you try the next
   piece of code and post the output here?
 * Regards
 *     ```
       function my_restrict_access() {		
           if ( is_product_category() ) {
               $category = get_queried_object();
   
               if ( $category ) {
                   echo '<pre>', print_r($category, 1), '</pre>';
   
                   $category_id = $category->term_id;
   
                   echo '1 = ' . $category_id . '<br>';
               }
           }
       }
       add_action( 'template_redirect', 'my_restrict_access');
       ```
   
    -  This reply was modified 6 years, 4 months ago by [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/#post-12380731)
 * Gives me a critical error on my site. [@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/#post-12380771)
 * [@thoseitguys](https://wordpress.org/support/users/thoseitguys/) hmm, can you
   try the updated code?
 * [https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12380675](https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12380675)
 *  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/#post-12383114)
 * This is the output I get [@crslz](https://wordpress.org/support/users/crslz/)
 * WP_Term Object
    ( [term_id] => 3078 [name] => X Fresh Local Deliveries to the
   Upper Highway only [slug] => fresh-local-deliveries [term_group] => 0 [term_taxonomy_id]
   => 3078 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 0[
   filter] => raw ) 1 = 3078
 * Also I dont need a redirect. I think to make it easier.
 * Is it possible to Hide the category completely if they dont have one of those
   3 postal codes?
 *  [crslz](https://wordpress.org/support/users/crslz/)
 * (@crslz)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/restrict-access-with-post-code/#post-12384112)
 * It looks like the category id now works without problems, so you can use this
   code.
 * You can of course always adjust this with other functionalities that meet your
   wishes.
 * Here you will find some examples: [https://businessbloomer.com/?s=categorie](https://businessbloomer.com/?s=categorie)
 * Regards
 *     ```
       function my_restrict_access() {		
           if ( is_product_category() ) {
               $category = get_queried_object();
   
               if ( $category ) {
       	    $category_id = $category->term_id;
   
       	    if( $category_id == 3076 ) {
       		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');
       ```
   

Viewing 15 replies - 1 through 15 (of 31 total)

1 [2](https://wordpress.org/support/topic/restrict-access-with-post-code/page/2/?output_format=md)
[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/2/?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