How to Display Visitor Location & IP Address on WordPress

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.

WordPress Visitor Location Information

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.nekudo.com/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 . "&deg;N <br />";
 $result .= "Longitude: " . $location_longitude . "&deg;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: France (FR)
City: Roubaix
IP: 167.114.238.112
Latitude: 50.6942°N
Longitude: 3.1746°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.114.238.112

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!

Twitter Button Facebook Button Google Button LinkedIn Button

Leave a Reply

Your email address will not be published. Required fields are marked *