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() {
 $url = "" . $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: United States (US)
City: Redwood City
Latitude: 37.4914°N
Longitude: -122.211°E
Accuracy radius: 1000km

If you only need to show the IP address, we can edit the code like so:

function visitorip_function() {
return = "<p>IP address: " . $ip . "</p>; }

add_shortcode('visitorip', 'visitorip_function');

And here is the shortcode “[visitorip]” in action:

IP address:

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!

9 thoughts on “How to Display Visitor Location & IP Address on WordPress”

    • 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!

    • Please, add this to your functions.php:

      function usercity_function() {
       $ip = $_SERVER["REMOTE_ADDR"];
       $url = "" . $ip;
       $json = file_get_contents($url);
       $data = json_decode($json, TRUE);
       $city = print_r($data['city'], true);
       $result = $city;
       return $result; }
       add_shortcode('usercity', 'usercity_function');
       add_filter( 'widget_text', 'shortcode_unautop' );
       add_filter( 'widget_text', 'do_shortcode' );

      Then use the shortcode: [usercity]

  1. 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’


Leave a Comment