khromov / wp-wpml-geoip-browser-language-redirect

GeoIP Redirector for WPML (WordPress)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to expose what Country Maxmind thinks you’re in

jonheslop opened this issue · comments

Hey

I wondered if there was a way to expose the country Maxmind returns so I can use it to show a flag. For example… Knowing the language is German doesn’t let me differentiate between Germany or Switzerland. So knowing the country would help here.

Provided you have the plugin installed, you should be able to do this in your themes functions.php

global $ipr;
$country_code = @geoip_country_code_by_addr($ipr->db, $_SERVER['REMOTE_ADDR']);

Now $country_code should hold your country code directly from MaxMind.

There is actually an example on customizing the list of flags over at WPML:
http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/

(Under "List of language names and flags for the footer")

Hope that helps.

Thanks for the quick reply
This errors badly… it just makes the page return entirely blank… no html at all and no errors.
Any ideas?

jon,

You need to enable WP_DEBUG to see errors.
http://codex.wordpress.org/Debugging_in_WordPress

Without it there's no meaningful way of knowing what went wrong.
However, I assume you have activated this plugin before trying the aformentioned code, because it won't work without it.

Hey I had debug mode on but the @ sign at the beginning of the function was suppressing the errors.

I couldn’t get it to work that way but then I looked into the Maxmind library baked into the plugin and got it working this way:

require_once( ABSPATH . '/wp-content/plugins/wp-wpml-geoip-browser-language-redirect/lib/geoip-api-php/geoip.inc' ); // For country lookup
$gi = geoip_open(ABSPATH . '/wp-content/plugins/wp-wpml-geoip-browser-language-redirect/database/GeoIP.dat',GEOIP_STANDARD); // IP Data
$countryCodeByMaxMind = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
$countryByMaxMind = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi); 

Thanks for your help

Happy that it worked out for you. You can download the standalone PHP library (same as this plugin uses) from the MaxMind homepage.