Show Visitor IP

Description

This plugin simply display Visitor IP Address & visitor location info using by visitor IP on post or page, anywhere using shortcode.

Very simple to install, simple to use, light weight.

External services

The location shortcodes ([svip_location]) need to resolve the visitor IP address into a location. To do that, the plugin sends the visitor IP address to a third party geolocation service over HTTPS. No other data is transmitted, and nothing is sent unless a location shortcode is actually used on the page. Because an IP address is personal data in some jurisdictions (for example under the GDPR), you should mention this in your own privacy policy.

Two services are used, neither of which needs an account or an API key:

  1. ipwho.is (primary) – https://ipwhois.io/ – terms: https://ipwhois.io/terms – privacy: https://ipwhois.io/privacy – 1,000 requests per day, commercial use allowed.
  2. freeipapi.com (fallback) – https://freeipapi.com/ – terms: https://freeipapi.com/terms – 60 requests per minute, commercial use allowed.

The fallback is only contacted when the primary is unreachable or does not carry the value a shortcode asked for. In practice ipwho.is answers everything except type="currency", which it does not provide, so a page that does not use the currency shortcode never contacts freeipapi.com at all.

Lookups are cached for 12 hours per IP address and shared between all shortcodes on a page, so a returning visitor causes no requests at all.

The plain [show_ip] shortcode does not contact any external service.

Page caching

The output of these shortcodes is different for every visitor. The plugin therefore sets the DONOTCACHEPAGE constant when a shortcode is rendered, which most caching plugins honour, so a page showing an IP address is not stored and served to other people. If your caching setup ignores that constant, exclude those pages from the cache manually.

Sites behind a proxy or CDN

By default only REMOTE_ADDR is used, because the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR headers can be forged by the visitor. If your site sits behind a reverse proxy, load balancer or CDN that sets those headers, opt in by adding this to wp-config.php:

define( 'SVIP_TRUST_PROXY_HEADERS', true );

Filters

  • svip_get_ip – filter the detected IP address.
  • svip_trust_proxy_headers – enable or disable the proxy headers.
  • svip_providers – reorder, remove or add geolocation services, for example a paid endpoint with an API key. Each entry is an array of url (with %s where the IP goes) and parse (a callback turning the decoded response into the plugin’s field names).
  • svip_request_timeout – change the per request timeout, 3 seconds by default.
  • svip_cache_lifetime – change how long a location lookup is cached.
  • svip_disable_page_cache – return false to stop the plugin setting DONOTCACHEPAGE.

Screenshots

Installation

Use WordPress Add New Plugin feature, searching “Show Visitor IP”, or download the archive.

  1. Log in to your website administrator panel
  2. Go to Plugins page, and add new plugin
  3. Upload plugin
  4. Click Install Now button
  5. Then click Activate Plugin button

Shortcodes: [show_ip] [svip_location] [svip_location type=”countryCode”] [svip_location type=”region”] [svip_location type=”regionCode”] [svip_location type=”lat”] [svip_location type=”long”] [svip_location type=”city”] [svip_location type=”countryName”] [svip_location type=”timeZone”] [svip_location type=”currency”]

The type attribute is not case sensitive. An unknown or missing type returns the country name.

FAQ

The location shortcodes show nothing on my local development site

Local and private network addresses (127.0.0.1, 192.168.x.x, …) cannot be geolocated, so nothing is returned. Test on a public site.

Everybody sees the same IP address

That is a page caching issue. See the “Page caching” section of the description.

The timezone shortcode is sometimes empty

The fallback service only reports timezones per country, so for countries that span several zones it cannot say which one a visitor is in. Rather than print a zone that may be hours out, the plugin leaves it empty in that case. It is only relevant while the primary service is unreachable.

Reviews

October 14, 2024
I’ve installed this plugin on multiple websites, and the shortcode is very easy to use.
November 2, 2023 2 replies
Doesn’t work. Fix it. Don’t post plugins that don’t work. [svip_location type=”city”] shows Country.
Read all 6 reviews

Contributors & Developers

“Show Visitor IP” is open source software. The following people have contributed to this plugin.

Contributors

Translate “Show Visitor IP” into your language.

Interested in development?

Browse the code, check out the SVN repository, or subscribe to the development log by RSS.

Changelog

5.3

  • Switched geolocation to ipwho.is with freeipapi.com as a fallback. Both are used over HTTPS and both allow commercial use, unlike the previous service, whose free endpoint was HTTP only and restricted to non-commercial use.
  • Requests now go through a provider layer, so a service being down or rate limited no longer means blank output, and sites can plug in their own service with the svip_providers filter.
  • Security: the visitor IP address is now validated and escaped before being printed, so a forged request header can no longer inject HTML or JavaScript into the page.
  • Security: the values returned by the geolocation service are escaped on output.
  • Security: the spoofable HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR headers are no longer trusted by default. Sites behind a proxy or CDN can opt in with the SVIP_TRUST_PROXY_HEADERS constant.
  • Only the first entry of an X-Forwarded-For chain is used, instead of the whole comma separated list.
  • Location lookups are now cached for 12 hours per IP and reused across shortcodes on the same page, instead of calling the geolocation API on every render. This keeps the site well inside the free rate limits.
  • Added type="regionCode" for the short region code, alongside the existing type="region" for the full name.
  • Added a 3 second timeout to the geolocation request, so a slow API cannot hold up page rendering.
  • Proper error handling for the geolocation request: WP_Error, non 200 responses and failed lookups are handled instead of producing PHP notices.
  • Fixed [svip_location] used without a type attribute causing a PHP error.
  • Fixed loose matching of the type attribute, where for example any value containing “lat” returned the latitude. Types are now matched exactly and are case insensitive.
  • The plugin now asks page caching plugins not to cache pages containing these shortcodes.
  • Functions and filters are prefixed with svip_ to avoid collisions. The old function names and the wpb_get_ip filter still work.
  • Added a direct file access guard, the missing plugin headers, and documentation of the external ip-api.com service.
  • Fixed the stable tag, which did not match the plugin version.

5.2 – 06 March 2024

  • Fixed the city shortcode issue.

1.0

  • Initial release.