Country specific content blocks for WordPress

applications-education-languageI created a new WordPress plugin called GeoBlock that allows you to show or hide content based on the visitor’s country. This plugin is not for people on shared hosting. It is for those of you who have root access to their own VPS or dedicated servers.

First up you have to install mod_geoip or the Nginx equivalent module for that. The instructions here are for configuring the Nginx version on a Debian system.

GeoIP support in Nginx

Run the following command to see if you have GeoIP support in your Nginx installation:

/usr/sbin/nginx -V &> nginx_v && grep geoip nginx_v > /dev/null && echo "Yes!" || echo "No"; rm nginx_v

If it says no then you have to install the appropriate Nginx GeoIP module using your distro’s package manager. Along with that it will install the free GeoIP database from Maxmind. For example on Debian I recommend using Dotdeb’s Nginx. It contains support for GeoIP lookups out of the box.

To configure Nginx to support GeoIP lookups you have to add this one line in the http{} block. I use the Dotdeb version of Nginx on a Debian system so I added it by creating a one line file in /etc/nginx/conf.d:

geoip_country /usr/share/GeoIP/GeoIP.dat;

Then make sure the environment variables are set if you are using fastcgi/PHP-FPM. Edit fastcgi.conf and add this:

fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

Restart Nginx for the changes to take effect. To make sure the environment variables are set create a one line PHP file in your document root:

<?php phpinfo();

If you view that PHP file in your browser it should show $_SERVER[‘GEOIP_COUNTRY_CODE’] set.

Installing the plugin

Download the latest zip file from the releases page and install it as you would any other WP plugin i.e. WP admin > plugins > add new > upload plugin.

Using the plugin

Wrap the shortcode [geoblock][/geoblock] around your content to restrict who can view it. For example to blacklist certain countries you would do this:

[geoblock type='blacklist countries='us,uk']
This block of content will not show up for visitors from the US and UK
[/geoblock]

The countries argument contains the 2 letter ISO codes of the countries you want to block separated by commas.

Change type to ‘whitelist’ in order to block content by default and only show it to people from certain countries.

To show a message when the content is blocked add a message argument:

[geoblock type='blacklist' countries='us,uk' message='Sorry but this content is not available in your region.']
...
[/geoblock]

You can use the message argument with a whitelist too.

The message argument to the shortcode cannot contain any html. It’ll be stripped out. If you want to show something more than a simple plain text message to people from blocked countries then use a combination of a blacklist block followed by whitelist block.

2 thoughts on “Country specific content blocks for WordPress

  1. Y can also remove emoji, if you don’t use them:
    remove_action(‘admin_print_styles’, ‘print_emoji_styles’);
    remove_action(‘wp_head’, ‘print_emoji_detection_script’, 7);
    remove_action(‘admin_print_scripts’, ‘print_emoji_detection_script’);
    remove_action(‘wp_print_styles’, ‘print_emoji_styles’);
    remove_filter(‘wp_mail’, ‘wp_staticize_emoji_for_email’);
    remove_filter(‘the_content_feed’, ‘wp_staticize_emoji’);
    remove_filter(‘comment_text_rss’, ‘wp_staticize_emoji’);

Leave a Reply

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