Adam
Adam

Reputation: 441

Fetching country from IP using wordpress plugin GeoIP detection?

I've installed this plugin: https://tah.wordpress.org/plugins/geoip-detect/

The plugin seems to work fine when I test "lookup" within the plugin, it returns my geo-information. However when I try to implement code within one of my wordpress pages it doesn't work.

$ip = $_SERVER['REMOTE_ADDR'];
$userInfo = geoip_detect2_get_info_from_current_ip($ip);
echo $userInfo->country->name;

I'm calling the function from a native woocommerce page where single-products are shown. But the fucntion returns nothing. Do I have to include anything more to call the function geoip_detect2_get_info_from_current_ip()?

I also tried:

<?php echo do_shortcode("[geoip_detect2 property='country']"); ?>

It doesn't return anything either. I'm doing the editing within godaddy's code editing tool, so I might be missing errors.

Upvotes: 1

Views: 7988

Answers (3)

Lars Koudal
Lars Koudal

Reputation: 1349

I have recently used the same plugin for a custom solution - I noticed the plugin have an IP detection function built in - geoip_detect2_get_client_ip() - try to use that instead?

Edited from your code:

$ip = geoip_detect2_get_client_ip();
$userInfo = geoip_detect2_get_info_from_current_ip($ip);
echo $userInfo->country->name;

This function has cache functionality built in as well, and in my testing it seemed very fast.

Upvotes: 1

Wedangsusu
Wedangsusu

Reputation: 156

I have try implement GEO by IP in my wordpress site http://iradiofind.com/stations/. I am using http://www.geoplugin.net to get the country info. To got the ip address i am using this function

function get_client_ip() {
     $ipaddress = '';
     if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
     else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
     else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
     else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
     else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
     else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
     else
        $ipaddress = 'UNKNOWN';

     return $ipaddress;
}

function ip_details($url) {
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   $data = curl_exec($ch);
   curl_close($ch);

   return $data;
}

And this is in my page template.

<?php 
    $myipd = get_client_ip(); 
    $url = 'http://www.geoplugin.net/json.gp?ip='.$myipd; 
    $details    =   ip_details($url); 
    $v = json_decode($details);
    $mycountry = $v->geoplugin_countryName;
?>

Upvotes: 3

metad00r
metad00r

Reputation: 134

Maybe the geoip-detect functions are out of your scope (something that would be very strange). Add this to your functions.php:

require_once ABSPATH . '/wp-content/plugins/geoip-detect/geoip-detect.php';

Upvotes: 0

Related Questions