Today I present you with another wordpress plugin that allows wordpress powered sites to easily detect the IP as well as the relevant country of the visitor so as to take appropriate action. While there might be other similar plugins, yet our solution is light weight, and relies on two web services (one main and another backup) making it a very straight forward an reliable approach.
Purpose of the plugin
The plugin essentially aims at providing the developer of wordpress solutions with ease of access to a function that connects to APIs of either freegeoip.net (as the main site) or alternatively www.geoplugin.net (as the backup service) to query for the country of the visitor based on the detected IP. Many times we receive requests from clients and/or developers for such functionality, so thought to make it easily accessible via this plugin.
Coding the plugin
The plugin essentially works in the background, and makes the function gk_map_ip_country available to be used across other code, so that when called anywhere, this function would allow grabbing the two letter identifier of the relevant visitor country.
The code is written as pure PHP and relies on $_SERVER content to identify the IP.
Utilizing the plugin
After setting up the plugin, anywhere in the code, particularly on front end functionality, the aforementioned function gk_map_ip_country() can now be called within code, and used to detect the relevant 2 letter relevant country code. Sample codes include 'US', 'CA', 'GB', 'IN',... and then decide upon which action needs to be taken. This can also be alternatively used to display the country for the visitor as well.
Downloading the plugin
The plugin is available on the official GitHub repository.
Being the sole contributor (and creator) of the plugin, there are currently no branches/forks/gists/pull requests, and all the code is openly accessible via above link. The most recent commit is accessible via this link
Feel free to check it out and use it. Let me know should you have any questions :)
Thank you
Posted on Utopian.io - Rewarding Open Source Contributors