Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Serving up content by location WordPress

I'm building a site that has a custom post type called 'offers'. The client wants to serve up different offers to US visitors and European visitors.

So, if you're in the UK, you'll see the following posts:

- Post 1 with taxonomy term 'europe'
- Post 2 with taxonomy term 'europe'
- etc.

And if you're in the US you'll see:

- Post 3 with taxonomy term 'usa'
- etc.

I'm thinking there must be a geolocation solution I can use alongside the post type's taxonomy to display the correct info to the right people, but I can't find anything off-the-shelf.

Any suggestions welcome


The idea of serving up different pages by country won't work because the content to be filtered by country is displayed in widget-type areas. On both homepage and blog posts we have offers in a sidebar widget .

Answers (5)


John Cotton answers:

I've used [[LINK href=""]]this[[/LINK]] on several occasions for exactly that sort of thing and have always found it good enough even though it's free.

There's some [[LINK href=""]]PHP code for querying the data here[[/LINK]]. It doesn't need a lot changing to work with $wpdb....


Marko Nikolic answers:

You can try these plugins:


designchemical answers:

You can use the geoplugin service. This returns serialized data of the users IP - e.g.:

a:14:{s:14:"geoplugin_city";s:8:"El Monte";s:16:"geoplugin_region";s:2:"CA";s:18:"geoplugin_areaCode";s:3:"626";s:17:"geoplugin_dmaCode";s:3:"803";s:21:"geoplugin_countryCode";s:2:"US";s:21:"geoplugin_countryName";s:13:"United States";s:23:"geoplugin_continentCode";s:2:"NA";s:18:"geoplugin_latitude";s:15:"34.076698303223";s:19:"geoplugin_longitude";s:16:"-118.02380371094";s:20:"geoplugin_regionCode";s:2:"CA";s:20:"geoplugin_regionName";s:10:"California";s:22:"geoplugin_currencyCode";s:3:"USD";s:24:"geoplugin_currencySymbol";s:5:"$";s:27:"geoplugin_currencyConverter";d:1;}

To use this see following example:

$meta = unserialize(file_get_contents(''.$_SERVER['REMOTE_ADDR']));
$meta_lat = $meta['geoplugin_latitude'];
$meta_lng = $meta['geoplugin_longitude'];
$meta_city = $meta['geoplugin_city'];


Paul Gregory answers:

[See edit below]

There are indeed geolocation services that you can use. They are not always accurate, and it is not always correct to assume that a user wants to see the offers for their internet service provider's location. Your client will probably wish to view both sets of offers from one location.

I would strongly recommend therefore that instead of coding up one page that shows a taxonomy based on the geolocation, you instead run a page for each term - /offers/europe and /offers/usa - and then have a redirect page at /offers which uses geolocation to take users to the relevant page. It will be easiest to redirect US traffic to /usa and All Other Traffic to /europe.

This is broadly the approach taken by many popular coupon sites and is much simpler to test and deploy.

(I doubt your client literally asked for a single page that showed US to US and European to Europe; what they probably expected was for users to click Offers and then see the offers relevant to their location.)

Each offers page should link to the other, for the cases when the location isn't appropriate - so for example vacationing Americans using European hotel wi-fi can easily switch to see their 'home' offers.

Your menu and banner links should always link to the redirect page.

If you want something off-the-shelf that will handle this redirection, I'll supply it if you're picking my answer. You simply need to create the US and Europe pages.


OK, so there are ALSO offers in sidebar widgets. So what you are looking for is something that will

a) Set a session variable to "USA" or "Europe" based on geolocation, if a session variable isn't already set.
b) Use that session variable directly as the taxonomy term.

What most of the other answers here are giving you is "show different content based on geolocation", which will often be querying geolocation on every page.

While it sounds like "match the geolocation to taxonomy" is simple, it isn't. Doing a plugin for this would be quite difficult as everyone's needs would vary.

Indeed, I'm not sure what the cleanest way of using a session variable as a taxonomy term is. Obviously it could be done in the PHP code for each usage, but I can see a number of use cases for a system whereby you entered 'auto-country' as the taxonomy term in your code, and something hooked into this to replace it with USA or Europe as appropriate. This would make using widget area plugins easier.

All in all, my revised approach would require access to your theme (and a copy of your website data) in order to complete. PM me if you want me to do this for you.


Arnav Joy answers:

try this plugin