Asking to recommend a product (plugin, theme, book, hosting provider), tool, library, or off-site resource is out of scope for this site, as it attracts opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 4 years ago.
Improve this questionI have a client who has multiple offices. Instead of listing all the locations out in the footer, they'd like to show the only one office which is closest to the user.
What would be the best solution for this? Is there a plugin which would easily handle this?
Closed. This question is off-topic. It is not currently accepting answers.Asking to recommend a product (plugin, theme, book, hosting provider), tool, library, or off-site resource is out of scope for this site, as it attracts opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 4 years ago.
Improve this questionI have a client who has multiple offices. Instead of listing all the locations out in the footer, they'd like to show the only one office which is closest to the user.
What would be the best solution for this? Is there a plugin which would easily handle this?
Share Improve this question asked Jan 11, 2021 at 13:35 HeweHewe 859 bronze badges 1- plugin recommendations are off topic here – Tom J Nowell ♦ Commented Jan 11, 2021 at 14:20
1 Answer
Reset to default 1It all depends on what accuracy you need to find out? World? State? City?
You can use Wordpress plugin some kind of IP Locator or Geolocation IP detection... Or you can use your own code to determinate IP address of visior. But it is could be difficult, because some providers have dynamic external IP and your found address is not precise even within multiple states.. I am from Poland and my provider is Orange, French company, and you can see my external IP as from French...
So you can use some AJAX methods to retrieve precise location as possible from client-side.
$.get("https://ipinfo.io/json", function (response) {
$("#ip").html("IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>
Or pure javascript without jQuery using https://geolocation-db service to call back IP location from client to server:
<!DOCTYPE html>
<html>
<head>
<title>Geo City Locator by geolocation-db</title>
</head>
<body>
<div>Country: <span id="country"></span></div>
<div>State: <span id="state"></span></div>
<div>City: <span id="city"></span></div>
<div>Postal: <span id="postal"></span></div>
<div>Latitude: <span id="latitude"></span></div>
<div>Longitude: <span id="longitude"></span></div>
<div>IP address: <span id="ipv4"></span></div>
</body>
<script>
var country = document.getElementById('country');
var state = document.getElementById('state');
var city = document.getElementById('city');
var postal = document.getElementById('postal');
var latitude = document.getElementById('latitude');
var longitude = document.getElementById('longitude');
var ip = document.getElementById('ipv4');
function callback(data)
{
country.innerHTML = data.country_name;
state.innerHTML = data.state;
city.innerHTML = data.city;
postal.innerHTML = data.postal;
latitude.innerHTML = data.latitude;
longitude.innerHTML = data.longitude;
ip.innerHTML = data.IPv4;
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://geoilocation-db/json/geoip.php?jsonp=callback';
var h = document.getElementsByTagName('script')[0];
h.parentNode.insertBefore(script, h);
</script>
</html>
Or can try to use PHP server-side library GeoIP2:
<?php
require_once("geoip2.phar");
use GeoIp2\Database\Reader;
// City DB
$reader = new Reader('/path/to/GeoLite2-City.mmdb');
$record = $reader->city($_SERVER['REMOTE_ADDR']);
// or for Country DB
// $reader = new Reader('/path/to/GeoLite2-Country.mmdb');
// $record = $reader->country($_SERVER['REMOTE_ADDR']);
print($record->country->isoCode . "\n");
print($record->country->name . "\n");
print($record->country->names['zh-CN'] . "\n");
print($record->mostSpecificSubdivision->name . "\n");
print($record->mostSpecificSubdivision->isoCode . "\n");
print($record->city->name . "\n");
print($record->postal->code . "\n");
print($record->location->latitude . "\n");
print($record->location->longitude . "\n");
$>
Try yourself which method suits your requirements... as I said it's all about exactly how you want to locate the visitor