Geo Text WordPress Plugin

Download version 2016-04-26

The GeoText plugin for WordPress allows you to display different content to your users depending on the country (or group of countries e.g. EU) they come from. Some example uses:

  • Displaying different affiliate links to people depending on what part of the world they come from
  • Preventing people from certain countries from viewing certain content due to legal restrictions
  • Adding legal disclaimers or VAT notices that will only show for people in relevant countries
  • Displaying fixed prices in different currencies that you’ve manually converted
  • Linking to the user’s country specific version of a site, e.g. wikipedia

To enable this functionality, the plugin provides two shortcodes – [geoText] and [geoContent]. Both shortcodes can be inserted to pages / posts using buttons in the text editor or tinyMCE editor.

The [geoText] shortcode is designed for displaying alternate pieces of text to visitors depending on what country or group of countries they come from. For example:

You are in [geoText us="the United States" eu="Europe"]the world[/geoText]

Would display You are in the United States to a visitor from the US, You are in Europe to a visitor from an EU member country, or You are in the world to anyone else.

The [geoContent] shortcode is designed for displaying larger blocks of content, including HTML, to only visitors from the specified countries or groupings. It is also useful when you have multiple countries without a grouping that you want to display the same content for. But it’s not as useful as [geoText] for just plain text. To replicate the previous example using [geoContent], it would look like this:

You are in [geoContent countries="us"]the United States[/geoContent][geoContent groupings="eu"]Europe[/geoContent][geoContent excludegroupings="eu" excludecountries="us"]the world[/geoContent]

Shortcode formats

Both shortcodes can be inserted using the buttons in the editor, meaning you don’t have to type out the shortcodes or remember the two digit country codes.

Inserting a geoText shortcode
Inserting a geoText shortcode

Inserting a geoContent shortcode
Inserting a geoContent shortcode

However, it is worth understanding the format used for both types of shortcode.

[geoText] takes the following format:

[geoText countryCodeOrGroupingName="content to show for this country or grouping"]Default content to show if none of the shortcode parameters match the user's country[/geoText]

[geoContent] takes the following format:

[geoContent countries="comma separated list of country codes that should be shown this content" groupings="comma separated list of groupings that should be shown this content" excludeCountries="comma separated list of country codes that should not be shown this content" excludeGroupings="comma separated list of groupings that should not be shown this content"]Content to be shown[/geoContent]

excludeGroupings and excludeCountries when used without the other attributes can be used to display content only to users that don’t match the excluded groupings / countries. For example, to display some content to everyone apart from those in the EU, you can use:

[geoContent excludeGroupings="eu"]This content is not for people in the EU[/geoContent]

A similar trick can be achieved with [geoText] by using an empty value:

[geoText eu=""]This content is not for people in the EU[/geoText]

Groupings

Groupings are a method that allows you to easily target multiple countries without having to type (or choose) a long list of countries. The plugin comes with two groupings covering countries that are members of the European Union (grouping eu) and countries that use the Euro as their currency (grouping eurocurrency).

There are two ways to add groupings (or alter existing groupings) – via the plugin settings page, or by editing a spreadsheet before first activating the plugin. When first activated, the plugin gets the list of groupings from the file countries_groupings_map.csv, which is in the plugin folder. You can edit this file in a text editor or spreadsheet software to add more groupings of your choice. Please note that all country codes must be in lowercase for them to work. The plugin only reads this spreadsheet on the initial activation, so if you want to add groupings by this method, you must do this before activating it.

Alternatively, you can add and alter groupings via the plugin settings page. You can manually type a list of comma separated country codes that each grouping should contain. Or you can use the drop-down list of country names to add the country codes that way. With the drop down list, each time you change the selected option, then it will add the newly selected country’s countrycode to the list of country codes for that grouping.

Geo Text plugin settings screen
Geo Text plugin settings screen

Please note that on a multisite installation, the groupings are shared between all sites. So if one site admin deletes or adds a grouping, that grouping will be deleted / added for all other sites as well. If this is a problem for you, please let me know.

Geo IP lookup

The plugin looks up a visitor’s country based on their IP address, however the plugin does not include this functionality itself. Instead it supports a number of external methods to do this (in order of preference):

If your WordPress installation does not currently have Geo IP lookup ability, I would recommend installing the WordPress GeoIP Detection plugin.

While the plugin will work with the ip2nation table, it is not recommended as some of the codes provided by the table do not appear to be correct ISO 3166 country codes.

If you’d like the plugin to use the Geo IP lookup provided by some other plugin or method not listed above, please contact me and I’ll see if I can add it.

Installation

  1. Unzip and upload the xoogu-geotext folder to the /wp-content/plugins/ directory.
    Or
    Click on the ‘Add New’ button at the top of the plugins management page in the wordpress admin area. On the next page click the ‘Upload plugin’ button. On the next page browse for the plugin zip file, then press the ‘Install Now’ button.
  2. Activate the plugin through the ‘Plugins’ menu in WordPress
  3. (Optional) In the WordPress admin dashboard, find Xoogu GeoText in the Settings menu to configure the plugin. You should check that the Geo IP detection method is set and working. If not, then install a GeoIP detection plugin. Note that if you are using multisite and network activate the plugin, then the plugin settings are only available from the network admin panel, not from the admin panels of the individual blogs.
Posted on by xoogu, last updated

19 Responses to “Geo Text WordPress Plugin”

  1. Mark jenner says:

    Hi,

    First of all, thank you for your work with the W3TC fragment caching and geo shortcodes….if I can get them to work they are exactly what I’m looking for!

    Anyway, I’ve been playing a while. Managed to get the fragment caching plugin working (I think) and am now trying to use the above shortcode plugin with your W3TC fragmented caching. I copy in your code from above to test the functionality:

    You are in [geoContent countries=”us”]the United States[/geoContent][geoContent groupings=”eu”]Europe[/geoContent][geoContent excludegroupings=”eu” excludecountries=”us”]the world[/geoContent]

    And all I get as an output to the webpage is the following:

    You are in if (!empty($GLOBALS[“shortcode_tags”][‘geoContent’])) {echo call_user_func( $GLOBALS[“shortcode_tags”][‘geoContent’], unserialize(stripslashes(‘a:1:{s:9:\”countries\”;s:2:\”us\”;}’)), ‘the United States’, ‘geoContent’ );}if (!empty($GLOBALS[“shortcode_tags”][‘geoContent’])) {echo call_user_func( $GLOBALS[“shortcode_tags”][‘geoContent’], unserialize(stripslashes(‘a:1:{s:9:\”groupings\”;s:2:\”eu\”;}’)), ‘Europe’, ‘geoContent’ );}if (!empty($GLOBALS[“shortcode_tags”][‘geoContent’])) {echo call_user_func( $GLOBALS[“shortcode_tags”][‘geoContent’], unserialize(stripslashes(‘a:2:{s:16:\”excludegroupings\”;s:2:\”eu\”;s:16:\”excludecountries\”;s:2:\”us\”;}’)), ‘the world’, ‘geoContent’ );}

    Any idea how I could get this working please?

    A test page i have set up (a copy of my genuine site where I will be using it) can be seen here:

    http://www.nomoreautomode.com/simply-the-rest/simple-geotargeting-test-for-fragment-caching/

    • xoogu says:

      Hi Mark

      I’ve found the issue with the PHP code being output, I think. I guess that you’re using HTML minification? In the W3TC settings, on the Minify page, scroll down to the ‘HTML & XML’ section. In the ‘Ignored comment stems:’ box, add ‘mfunc’ (without the quotes) to the list of ignored comments. Then clear the page cache (and your browser’s cache) and try again, and it should work.

      I’m kind of surprised that mfunc isn’t included as a default ignore comment. I’ll see if I can modify my plugin to add that to the comment ignore list automatically. If not, I’ll have to put a note in the instructions. And of course, I’ll fix the problems with the buttons in the editor not working.

      Thanks again for letting me know about these problems.

      Dave

      • Mark Jenner says:

        Hi Again,

        Yes, that’s stopped the PHP being output and after some reading elsewhere I can see how that works as in the PHP there are mfunc comments that the caching skips and if the tag is being stripped the code will be executed once then cached instead of skipped. Makes perfect sense!

        Still not quite working for me though. From the shortcode example:

        You are in [geoContent countries=”us”]the United States[/geoContent][geoContent groupings=”eu”]Europe[/geoContent][geoContent excludegroupings=”eu” excludecountries=”us”]the world[/geoContent]

        I only ever get output: You are in the united States

        Tried from UK where I am, as well as a proxy and a VPN, and a friend tried in Malta also, same output.

        I am surprised how hard it is with caching to show different content to people 🙁 Been searching for a solution for many days.

      • Mark Jenner says:

        If I remove the US shortcode and write only the following:

        You are in [geoContent groupings=”eu”]Europe[/geoContent][geoContent excludegroupings=”eu” excludecountries=”us”]the world[/geoContent]

        I get no output, only:

        You are in

        No Europe or the world, whether I use a proxy or VPN etc.

        • xoogu says:

          Hi Mark

          Not sure if you saw my email, but can you let me know what Geo IP method you’ve set in the plugin settings?

          And underneath the Geo IP method option in the plugin settings, it should say ‘Your country code is xx’ (where ‘xx’ is your country code). Does it give the correct code here, or does it say ‘us’ also?

          Finally, are you using object caching or database caching in W3TC also?

          Thanks

          Dave

  2. Mark jenner says:

    Hi,

    I also notice the shortcode buttons in the WP visual editor have no effect. Is there a reason for this?

  3. Mark jenner says:

    Oh, I’ve just seen this: https://www.w3-edge.com/solutions/w3-total-cache-pro/

    Do you need pay for W3TC pro in order for any fragment caching to work?

    • xoogu says:

      There’s two types of fragment caching W3TC supports, the one my extension uses is the type that comes with the free version of W3TC. So you don’t need to pay for the pro version.

      The shortcode buttons issue is definitely a problem with my plugin.

      The issue with the PHP code being output when using the W3TC dynamic shortcodes extension I haven’t been able to test yet. W3TC on my test installation is not caching pages at all, so I need to try and debug that first of all. When I’ve got W3TC working normally, then I’ll be able to test it with the extension and the geoText plugin and see if I can recreate the issue.

  4. Mark Jenner says:

    Hi X,

    A bit of feedback that might help others. I struggled on with your plugins for a while, then gave up on it essentially. I just came back to it and gave it another try.

    While I was working, I checked hits in my pretty link lite where I saw that ALL website hits were coming from my own server IP. So my website wasn’t seeing visitors originating IP.

    After speaking with support, I just had to add some code into my wp-config.php file:

    /* Allows IP to be passed from remote host to server */
    if (isset($_SERVER[“HTTP_X_REAL_IP”]))
    $_SERVER[“REMOTE_ADDR”] = $_SERVER[“HTTP_X_REAL_IP”];

    I’ve now changed to other plugins and have geotargeting working 🙂

    Out of curiosity, I uploaded a backup of my site with the implementation of your plugins to a hosted test environment I have, and tried again. Lo and behold, it worked!

    So your plugins worked fine after following your instructions. The problem was host IPs were never being passed to my server for whatever reason.

    So anybody else using your plugins might want to check for this (I’m sure the same would happen if using reverse proxy, or cloudflare and so on.)

    So yes, just thought I’d let you know your plugins and instructions worked fine, however my server set=up did not 🙂

    Thanks and all the best!

    Mark.

    • xoogu says:

      Thanks for the feedback. I should be able to alter the plugins so that they check to see if $_SERVER["HTTP_X_REAL_IP"] is set, and if it is, then use that as the IP for getting the geo-location from.

  5. Andres Molina says:

    I could not beleive there where no such a plugin out there, i have to thank you A LOT for this, as it is just what i needed. Thanks so much man, i took me 5 hours to get to your site, but it worth the time.

    Why not publish it on wordpress plugins?

    • xoogu says:

      Hi Andres

      Thanks, glad the plugin is useful for you. Unfortunately the WordPress plugin repository is quite difficult to get plugins approved for and the reasons why a plugin is rejected can be rather vague or sometimes stupid (IMO). I say this based not just on my own experience of submitting a plugin, but also based on other plugins I use that are suddenly removed from the repository by the WP admins / mods for silly reasons.

      Additionally, I’m not a big fan of being forced to licence code under the GPL, which means anyone can just take your plugin and resell it for a profit as-is.

      So it’s much easier for me to just offer the plugins here on my own site.

      Dave

      • Andres says:

        Hey,

        Just an “improovment”. When adding countries, if by mistake you input twice same country, it says it has saved it, but the truth is that it has delete the whole countries config. I guess you can make so if somene repeats a country, just delete/ignore the repeated one.

        Thanks
        Kind regards

        • xoogu says:

          Thanks for the report! I’ve just updated it to fix that bug now. Let me know if you spot any more problems or things that could be improved.

          Cheers

          Dave

          • Andres says:

            Well,

            Now that you are asking for improvements, I got a suggestion for you.

            Right now im using your plugin on a woocommerce site, which in some countries are blocked from buying. So if you are located in one of the blocked countries it will show a message such as “Buying is not allowed in your country”. Then i got another rule, which is set to countries where you CAN purchase, that shows a message saying “RECOMMENDED RETAIL PRICE”.

            This could be done in an easier way i guess. with a typical if *** else if.

            So if you do not belong to the first range of locations, then show this other message. That way I would not have to configure manually all the non accepted languages, so i will create one rule saying if you are located in this 5 countries show recommended retail price, if any other location show buying is not allowed.

            Thanks
            Kind regards

          • xoogu says:

            Hi Andres

            Unless I’m misunderstanding what you’re looking for, that can be done using the plugin like:

            [geoText blocked-countries="Buying is not allowed in your country"]RECOMMENDED RETAIL PRICE[/geoText]

            Anyone in the blocked-countries grouping would see ‘Buying is not allowed in your country’, while anyone not in a blocked country would see the default text of ‘RECOMMENDED RETAIL PRICE’.

            Or with geoContent, by using two shortcodes – one to include the grouping that you want, and the other to exclude it, e.g.
            [geoContent excludeGroupings="blocked-countries"]RECOMMENDED RETAIL PRICE[/geoContent]
            [geoContent groupings="blocked-countries"]Buying is not allowed in your country[/geoContent]

            Dave

  6. andres says:

    Hi again Dave,

    Last time we talked about, you told me about improovements. So I got one for you that some or most people will think is FAVULOUS, but im not sure if it is possible to do.

    Do you think you can put an option in the plugin to be able (or not, just like a checkbox) to do not hide the text or use the shortcodes for Google bot?

    Im telling you this, because in my case and i guess in some other people cases, google bots are in the US and i dont want to hide them the info that im hiding behind your plugin.

    You think this is something that can be done? something you will be hapy doing?

    I know i told you MANY times, but once more is not going to kill… THANKS A LOT FOR YOUR PLUGIN, WITHOUT IT I WOULD NOT BE ABLE TO RUN MY SITE THE WAY I AM.

    kind regards

Leave a Reply