1

I am building a site that has pages that have a contact pod that has 11 different dropdowns in wp-admin via Advanced Custom Fields.

Based on the user's IP, I want to display one of those 10 contacts (the 11th one is a default if location isn't given or it doesn't match one of the client's "regions").

The 10 regions are all based on parts of the United States, but don't match to states all the time (for example California is split into two regions, while another region may include Virginia, South Carolina and North Carolina). Anyone outside of these regions would get a default/international contact/message.

I'm having a hard time wrapping my head around how to do this. I've got the Advanced Custom Fields in wp-admin setup and am comfortable with outputting items from that plugin, but I am struggling with:

  1. What to use as a database to check IPs against. Is there anything off the shelf to do this?
  2. How to create my "buckets" of IPs for each region.
  3. How to setup the logic to choose which region contact/text to output.
1

I would suggest checking out a geolocation service to lookup the location of the users IP address, such as freegeoip.net. In order to do this you would need to make a rest api get request either through php or javascript.

To get the ip address of the visiting user you can read in

<?php echo $_SERVER['REMOTE_ADDR']; ?>

then send that off to the restful api at freegeoip.net. Free geoip will give you plenty of daily lookups and can give you the following information as an example from the json api:

{
"ip": "104.37.65.178",
"country_code": "CA",
"country_name": "Canada",
"region_code": "ON",
"region_name": "Ontario",
"city": "Burlington",
"zip_code": "L7M",
"time_zone": "America/Toronto",
"latitude": 43.4191,
"longitude": -79.8544,
"metro_code": 0

}

As you can see you can get the latitude and longitude of the estimated location, as well the region, country, city, and estimated zip codes. Once you have that information, then it's a matter of checking if the states match your defined regions without requiring the use of "bucket lists" of ip ranges.

I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.