Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am designing an events site and would like people to see a homepage based upon the events that are in their area.

Could this could also be done with a login, if they register their address?

share|improve this question

2 Answers 2

  • The modern way would be to use html5 which does support geolocation: simple demo
  • A bit longer explanation how this could be added to a web-app here
  • A nice php script that uses a web service called geoplugin (the api is nice, no idea about the company, ymmv!)
  • And last but not least, using the html5 geolocation from php (which you would need for wordpress) at stackoverflow

The latter has a nice explanation how to use modernizer which could then combine html5/geolocation with some webservice or something.

share|improve this answer

It is possible to get very fuzzy location information for most people, most of the time by using the IP address and a geoIP service. I say "fuzzy" because what you frequently get is the location of the ISP's servers, not the specific location of the particular IP address. For example, from home my IP comes up listed as being about 30-40 miles away from where I actually am. Also, some people may be visiting through a proxy which could be hundreds or thousands of miles away from their actual location. If those are acceptable parameters, then something like...

$IP = $_SERVER['REMOTE_ADDRESS'];
// get location data
// I am using freegeoip.net for this
// I can't vouch for the service. It is just an example,
// but I queried my IP and it worked well.
$location = wp_remote_get("http://freegeoip.net/json/{$IP}");
// var_dump($location);
// location information is in $location['body']
// check status
if ('200' == $location['response']['code']) {
  $geoloc = json_decode($location['body']); 
  var_dump($geoloc);
  // and you get this
  // stdClass Object
  // (
  //   [city] => xxx
  //   [region_code] => xx
  //   [region_name] => xx
  //   [metrocode] => xxx
  //   [zipcode] => xxxxx
  //   [longitude] => xxxxxx
  //   [latitude] => xxxxxx
  //   [country_code] => xx
  //   [ip] => xxx.xxx.xxx.xxx
  //   [country_name] => xxxxx
  // )
  // you know have what you need to switch your homepage content
}

freegeoip.net limits requests to a 1000 per hour, so be careful that you run the code only when necessary-- set a cookie, something. I'd recommend caching the data to reduce requests count and to reduce the overhead/lag of contacting a third party service.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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