Title: problem in functions.php
Last modified: August 22, 2016

---

# problem in functions.php

 *  Resolved [Morteza Rahmani](https://wordpress.org/support/users/parsilinux/)
 * (@parsilinux)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/)
 * Hi.
    i am using your plugin to block users from outside my country (Iran) submitting
   forms in my site. I am using IP2Location Tags & Gravity Forms. so here is my 
   functions.php file:
 *     ```
       <?php
       // Adding IP2Location DB for IP Geo Block
       function my_ip2location_path( $path ) {
       	return WP_CONTENT_DIR . '/ip2location-tags/IP2LOCATION-LITE-DB5.BIN';
       }
       add_filter( 'ip-geo-block-ip2location-path', 'my_ip2location_path' );
   
       // Set $validate to passed only if country is Iran (ir)
       function my_whitelist( $validate ) {
       	$whitelist = array(
       	'ir',
       	);
       	$validate['result'] = 'blocked';
       	foreach ( $whitelist as $country ) {
       		if ( strtoupper( $country ) === $validate['code'] ) {
       			$validate['result'] = 'passed';
       			break;
       		}
       	}
       	return $validate;
       }
   
       // Validate Gravity Forms only if country of users is Iran
       add_filter("gform_validation", "ip_validation", 10, 4);
       function ip_validation($validation_result){
       	if ($validate == "passed") {
                   $validation_result["is_valid"] = true;
               }
               else{
       			$validation_result["is_valid"] = false;
               }
           return $validation_result;
       }
       add_filter('gform_validation_message', 'change_validation_message', 10, 2);
       function change_validation_message($message, $form)
       {
       return "<div class='validation_error'>You can't submit this form outside of Iran. Thank you</div>";
       }
       ?>
       ```
   
 * but it doesn’t work.
    when i do var_dump($validate); , it return NULL. how should
   i fix it?
 * thank you.
 * [https://wordpress.org/plugins/ip-geo-block/](https://wordpress.org/plugins/ip-geo-block/)

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Plugin Author [tokkonopapa](https://wordpress.org/support/users/tokkonopapa/)
 * (@tokkonopapa)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923085)
 * Thank you for using my plugin.
 * This plugin does nothing on regular page where the form is shown. But you may
   be happy if you can get the country code of your visitor in `ip_validation()`.
 * How about the following code?
    (I omit the IP2Location DB setting.)
 *     ```
       // Validate Gravity Forms only if country of users is Iran
       add_filter( 'gform_validation', 'ip_validation' );
       function ip_validation( $validation_result ) {
       	$whitelist = array(
       		'IR',
       	);
   
       	$validation_result["is_valid"] = true;
   
       	if ( class_exists( 'IP_Geo_Block' ) ) {
       		$validation_result["is_valid"] = false;
   
       		$geolocation = IP_Geo_Block::get_geolocation( $_SERVER['REMOTE_ADDR'] );
       		if ( !empty( $geolocation['countryCode'] ) {
       			foreach ( $whitelist as $country ) {
       				if ( $country === $geolocation['countryCode'] ) {
       					$validation_result["is_valid"] = true;
       					break;
       				}
       			}
       		}
       	}
   
       	return $validation_result;
       }
       add_filter( 'gform_validation_message', 'change_validation_message', 10, 2 );
       function change_validation_message( $message, $form ) {
       	return "<div class='validation_error'>You can't submit this form outside of Iran. Thank you</div>";
       }
       ```
   
 *  Thread Starter [Morteza Rahmani](https://wordpress.org/support/users/parsilinux/)
 * (@parsilinux)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923160)
 * Dear tokkonopapa
    thank you for your answer & sorry for my late reply.
 * I tried your code.
    after this line :
 *     ```
       $geolocation = IP_Geo_Block::get_geolocation( $_SERVER['REMOTE_ADDR'] );
       ```
   
 * when i **print_r($geolocation)**, it successfully returns an array like this:
 *     ```
       Array
       (
           [ip] => 2.176.231.191
           [auth] => 1
           [time] => 0.0028219223022461
           [provider] => Cache
           [errorMessage] => not in the cache
       )
       ```
   
 * but **$geolocation[‘countryCode’]** returns **NULL**. so it doesn’t work as expected.
 *  Plugin Author [tokkonopapa](https://wordpress.org/support/users/tokkonopapa/)
 * (@tokkonopapa)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923163)
 * > thank you for your answer & sorry for my late reply.
 * That’s OK! I appreciate you that you are kindly giving me a chance to improve
   this plugin.
 * I think there’s some problems related to IP address database settings. Please
   check followings on this plugin page.
 * 1. Go to the ‘**Settings**‘ tab. Check ‘**IP2Location**‘ in ‘**API selection 
   and key settings**‘. If you can’t find ‘**IP2Location**‘, then this plugin can’t
   find the DB.
    2. Go to the ‘**Search**‘ tab. Select ‘**IP2Location**‘ in ‘**Geolocation
   service**‘ and search your IP address. If something is wrong, you will see ‘errorMessage’
   on the map.
 * And please try to use another DB, at least Maxmind.
 * 3. Go to the ‘**Settings**‘ tab. Check ‘**Maxmind**‘ in ‘**API selection and 
   key settings**‘. Then push ‘**Download now**‘ in ‘**Maxmind GeoLite settings**‘.
   If something is wrong, you will see an error message.
 * I strongly recommend to use multiple DBs. If you find any errors, please report
   here.
 * Thanks.
 *  Plugin Author [tokkonopapa](https://wordpress.org/support/users/tokkonopapa/)
 * (@tokkonopapa)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923165)
 * Additional Info.
    If all of DBs are not available, this plugin will return the
   followings.
 *     ```
       Array
       (
           [ip] => xxx.xxx.xxx.xxx
           [auth] => 1
           [time] => 0.000348091125488
           [provider] => Cache
           [errorMessage] => not in the cache
       )
       ```
   
 * or
 *     ```
       Array
       (
           [ip] => xxx.xxx.xxx.xxx
           [auth] => 1
           [time] => 0.000137090682983
           [provider] => Cache
           [countryCode] => ZZ
       )
       ```
   
 * A **[countryCode] => ZZ** means ‘unknown’ IP address.
 *  Plugin Author [tokkonopapa](https://wordpress.org/support/users/tokkonopapa/)
 * (@tokkonopapa)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923190)
 * I’m sorry about your trouble but my `sample.php` was not good.
 * Could you try following codes to set the DB for ip2location-tags?
 *     ```
       function my_ip2location_path( $path ) {
           return WP_CONTENT_DIR . '/plugins/ip2location-tags/IP2LOCATION-LITE-DB5.BIN';
       }
       add_filter( 'ip-geo-block-ip2location-path', 'my_ip2location_path' );
       ```
   
 * You may need the `plugins`.
 *  Thread Starter [Morteza Rahmani](https://wordpress.org/support/users/parsilinux/)
 * (@parsilinux)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923197)
 * hi.
    thanks for your time. i replaced your new code. now in IP Geo Block Setting&
   Search Tabs, i can access ip2location db and it works great.
 * but in functions.php, when i try this:
 *     ```
       <?php
       function my_ip2location_path($path) {
           return WP_CONTENT_DIR . '/plugins/ip2location-tags/IP2LOCATION-LITE-DB5.BIN';
       }
       add_filter('ip-geo-block-ip2location-path', 'my_ip2location_path');
       if (class_exists('IP_Geo_Block')) {
       		$geolocation = IP_Geo_Block::get_geolocation($_SERVER['REMOTE_ADDR']);
       		print_r($geolocation);
       		}
       ?>
       ```
   
 * it still returns an array without country code.
 *     ```
       Array
       (
           [ip] => xxx.xxx.xxx.xxx
           [auth] => 1
           [time] => 0.000348091125488
           [provider] => Cache
           [errorMessage] => not in the cache
       )
       ```
   
 * I also tried to set **Number of entries** & **Expiration time** in Cache Settings
   to **0** to disable Cache. but didn’t work.
 *  Plugin Author [tokkonopapa](https://wordpress.org/support/users/tokkonopapa/)
 * (@tokkonopapa)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923226)
 * Sorry but I found a bug and fixed `not in the cache` issue.
 * Please update to the version 2.0.4 and try following codes.
 *     ```
       <?php
       // Adding IP2Location DB for IP Geo Block
       function my_ip2location_path( $path ) {
       	return WP_PLUGIN_DIR . '/ip2location-tags/IP2LOCATION-LITE-DB5.BIN';
       }
       add_filter( 'ip-geo-block-ip2location-path', 'my_ip2location_path' );
   
       // Validate Gravity Forms only if country of users is Iran
       add_filter( 'gform_validation', 'ip_validation' );
       function ip_validation( $validation_result ) {
       	$whitelist = array(
       		'IR',
       	);
   
       	$validation_result["is_valid"] = true;
   
       	if ( class_exists( 'IP_Geo_Block' ) ) {
       		$validation_result["is_valid"] = false;
   
       		// default IP address is $_SERVER['REMOTE_ADDR']
       		$geolocation = IP_Geo_Block::get_geolocation();
   
       		// if found
       		if ( empty( $geolocation['errorMessage'] ) {
       			if ( in_array( $geolocation['code'], $whitelist ) ) {
       				$validation_result["is_valid"] = true;
       			}
       		}
   
       		// if not found
       		else {
       			$validation_result["is_valid"] = true; // or false
       		}
       	}
   
       	return $validation_result;
       }
       add_filter( 'gform_validation_message', 'change_validation_message', 10, 2 );
       function change_validation_message( $message, $form ) {
       	return "<div class='validation_error'>You can't submit this form outside of Iran. Thank you</div>";
       }
       ?>
       ```
   
 * Thank you for your trying so many times!
 *  Thread Starter [Morteza Rahmani](https://wordpress.org/support/users/parsilinux/)
 * (@parsilinux)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923230)
 * Dear tokkonopapa
    Thank you very much for your amazing support! I installed latest
   version and now everything works like a charm (Also Gravity Forms Save/Update
   issue is now fixed) 🙂
 *     ```
       Array
       (
           [ip] => xxx.xxx.xxx.xxx
           [auth] => 1
           [time] => 0.004810094833374
           [provider] => IP2Location
           [code] => IR
       )
       ```
   
 * best wishes to you.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘problem in functions.php’ is closed to new replies.

 * ![](https://ps.w.org/ip-geo-block/assets/icon-128x128.png?rev=1148568)
 * [IP Geo Block](https://wordpress.org/plugins/ip-geo-block/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ip-geo-block/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ip-geo-block/)
 * [Active Topics](https://wordpress.org/support/plugin/ip-geo-block/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ip-geo-block/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ip-geo-block/reviews/)

## Tags

 * [ip2location](https://wordpress.org/support/topic-tag/ip2location/)

 * 8 replies
 * 2 participants
 * Last reply from: [Morteza Rahmani](https://wordpress.org/support/users/parsilinux/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/problem-in-functionsphp-1/#post-5923230)
 * Status: resolved