May 22, 2011

Region detect wordpress plugin

Filed under: PHP and MySQL,Wordpress — Joe Brewer @ 2:57 pm

Region detect is a plugin for WordPress that detects the country that the current visitor is browsing from. It can display the country to the user; something like:

Hello visitor from %%COUNTRYNAME%%

This is done by using the codes %%COUNTRYCOD E%% and %%COUNTRYNAM E%% in your posts (take out the spaces).

The plugin makes the two character ISO 3166-1 country code and country name available to other plugins – just declare the global variables $rdISO3166_1 and $rdCountryName.

Region detect options

Region detect options screen

Default country code
This is the code that will be used if the plugin cannot determine the country from the currently selected service.

URL Parameter
This is the url parameter key. When the plugin is first installed it will default to “country” and will allow the country to be set like this:{two character country code}.

Override IP service with url parameter
This option forces the plugin to check the url parameter before the selected service below. If caching is switched on, this option will overwrite the IP in the cache if it exists.

IP address to country service
This plugin currently only supports MaxMind’s country lookup service. MaxMind offer a downloadable database file which is updated monthly and a web service which always provides current data without having to update any local files. MaxMind’s website can be found here *.

License key
This is used for the MaxMind license key – this is only used when you select the MaxMind web service using the option above.

Database file
This combo box lists all of the MaxMind database files that are in the plugin folder.

Cache ip addresses
This option allows the ip addresses and country codes to be temporarily stored. This only needs to be turned on when the web service has been selected and will slow down the number of ip address lookups that are performed. When the cache is switched on, a table will be displayed below the options panel showing how many ip addresses are being cached:

Region detect cache screen

Thanks to James Gill at GoSquared for allowing me to use his Flag icons in this plugin.

Cache length
This option determines the length of time that the ip addresses are temporarily stored for.

Search engine robot exclusion rules

Search engine exemption rules screen

This option is for stopping search engine robots from causing the plugin to lookup an ip address. If the web page is not being requested by a human being, then it is not worth looking up the address. This works by looking at the user agent and seeing if they contain any of the rules. Common user agents include:

Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Opera/9.01 (Windows NT 5.1; U; en)

Check search engine bot rules
This option logs all useragents, the number of hits and the number of exclusions. Use this option for checking the rules and whether new rules need to be added.

Useragent logging screen

* This is an affiliate link.


  1. Hey Joe, great plugin, thanks!

    First of all, I’m relatively new to WP plugin development. Regarding the global variables made available to other plugins, I’m not having any success referencing those variables ($rdISO3166_1 and $rdCountryName). For example, if I print or echo $rdCountryName in another plugin (for example, the Hello Dolly plugin), I see that the variable is empty.

    How does one go about referencing these two global variables?

    Thanks again!

    Comment by Robert Dyson — June 14, 2011 @ 11:59 am

  2. Hi Robert. Thanks for the kind words. The most likely issue is that you are not declaring the variables with the global keyword before you use them.

    Comment by Joe Brewer — June 14, 2011 @ 4:38 pm

  3. That was the issue, thanks for the quick reply Joe!

    Declaring the vars and then printing/echoing works in other plugins, as well as in theme files (header, footer, etc.)

    global $rdISO3166_1, $rdCountryName;
    echo $rdISO3166_1 . ‘ – ‘ . $rdCountryName;

    Comment by Robert Dyson — June 15, 2011 @ 9:02 am

  4. Is there a way to get the country code printed directly i the theme?
    Something like that maybe?

    $country =

    Comment by Victor — June 14, 2012 @ 8:28 pm

  5. Hi Victor. To access the country code in your theme, declare $rdISO3166_1 as a global then that variable will contain the country code.

    Comment by Joe Brewer — June 23, 2012 @ 2:43 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress