These are simple instructions on how to display visitor location information (such as country, city, coordinates) and visitor’s IP address inside WordPress content; post, pages, widgets — without the use of a plugin.
It’s easy but you’ll need to know how to edit your functions.php file.
This method uses a free Geolocation API to fetch location information from MaxMind’s “geolite2 database” based on visitor IP address. The information can be show in pages, posts and html/text widgets using a simple shortcode.
- If you only want to show the IP address there is no need for the API — and I’ve included and example of a simpler shortcode function for that.
- If you want to show different content based on the location data (such as the country location of a visitor); please see this post.
Add the following to the end of your functions.php:
function visitorlocation_function() { $ip = $_SERVER["REMOTE_ADDR"]; $url = "https://geoip.nekoapi.icu/api/" . $ip; $json = file_get_contents($url); $data = json_decode($json, TRUE); $country_name = print_r($data['country']['name'], true); $country_code = print_r($data['country']['code'], true); $city = print_r($data['city'], true); $location_accuracy = print_r($data['location']['accuracy_radius'], true); $location_latitude = print_r($data['location']['latitude'], true); $location_longitude = print_r($data['location']['longitude'], true); $ip = print_r($data['ip'], true); $result = "<p>Country: " . $country_name . " (" . $country_code . ") <br />"; $result .= "City: " . $city . "<br />"; $result .= "IP: " . $ip . "<br />"; $result .= "Latitude: " . $location_latitude . "°N <br />"; $result .= "Longitude: " . $location_longitude . "°E <br />"; $result .= "Accuracy radius: " . $location_accuracy . "km </p>"; return $result; } add_shortcode('visitorlocation', 'visitorlocation_function');
Then add the shortcode [visitorlocation] inside the contents of a WordPress post or page — and if you want to include it in a text/html widget;
Please add the following two lines to your functions.php:
add_filter( 'widget_text', 'shortcode_unautop' ); add_filter( 'widget_text', 'do_shortcode' );
And here you can see the shortcode “[visitorlocation]” in action:
Country: United States (US)
City: New York
IP: 167.71.118.31
Latitude: 40.7584°N
Longitude: -73.9794°E
Accuracy radius: 1000km
If you only need to show the IP address, we can edit the code like so:
function visitorip_function() { $ip = $_SERVER["REMOTE_ADDR"]; return = "<p>IP address: " . $ip . "</p>; } add_shortcode('visitorip', 'visitorip_function');
And here is the shortcode “[visitorip]” in action:
IP address: 167.71.118.31
Good to know:
- Note that you need to edit the word ‘visitorlocation’ or ‘visitorip’ three times in the code to change the name of the shortcode.
- If you are seeing the server IP address instead of the visitor IP, try to change [“REMOTE_ADDR”]; to [“HTTP_X_REAL_IP”]; — to pass any proxy/firewall.
And that’s it to display location info on WordPress — easy peasy way.
And please mention this post or share if you found it useful.
Thank you — & comments welcome!
Hi: This is great. Is there a way to display the IP location within the “Comments” area of the WordPress Dashboard? Currently, when someone leaves a comment and I manage the comments via the Comments Dashboard, it lists the IP address of that commentor. I’d like to have that also show the location in that interface. See image below:
https://www.dropbox.com/s/o1xbmika3uije31/Screenshot_2017-10-14_10_50_02.png?dl=0
Good question but the code above is intended to simply fetch the IP location of the person who is viewing the page. For multiple IP addresses it would require much more complex code. Great idea worth considering though!
Hello,
How would I modify the code to show just a user’s city in a page or post?
Thanks so much!
Please, add this to your functions.php:
Then use the shortcode: [usercity]
That was so cool, than you for sharing
use ‘wp_remote_fopen($url);’ instead of ‘file_get_contents($url);’
Hats off , Thank you so much
Hi,
Is possible to do the same to display the user’s state?
What line of code can you add to the script if the person’s location is not available or turned off? If false uses this variable – ‘your city’