WordPress Plugin: “Block Countries”

April 16, 2007 by · 66 Comments 

As was mentioned in my "To the jerk stealing my content" rant, I've recently developed a WordPress plugin that can be used to block certain countries from accessing a blog. After a few days of tweaking, I believe that it's finally ready for a public release. I give you... "Block Countries"!

I'm actually surprised at how quickly this plugin was completed. I started with ZERO idea as to how to build a plugin for WordPress but within a couple hours I had a bare-bones plugin that accomplished what I needed it to do. Of course, it then took most of the weekend to clean it up to the point where I would feel comfortable telling someone, "Why don't you try this out on your blog?".

The zip file with the plugin can be found at the bottom of this post. You'll need to unzip the archive into your "wp-content/plugins" directory and then download the newest IP-to-Country CSV file from IP-to-Country.com (direct link to download page) and unzip that to the "block-countries" folder that should have been created inside your "plugins" folder when you unpacked my plugin.

The next step for installation on your blog is to enter wp-admin and enable the plugin on the "Plugins" page. When you do that, an additional table should automatically be created in your wordpress database. This table is going to be used to store the data from the above-mentioned CSV file for faster access.

After the plugin has been activated, select the "Options" menu item, followed by "Block Countries". Assuming that you successfully uploaded both the plugin PHP file and the CSV data file, you should receive a message to the effect of, "the table is empty but that the CSV was found". If that is the case, click the "Update" button and the CSV should be loaded into your database.

Once the screen refreshes, you should have access to 3 true/false option boxes and one large list box filled with a list of 200 or so country names. At this point, all that remains to do is to activate the scanning options by changing their values to "True" (the top one should catch +95% of users, the last two are to try and block users using proxy servers) and then to select which countries you wish to ban from your blog pages. Click "Update" again once you're done.

One thing worth mentioning, although I doubt that this will affect many of you, but if your site is behind a reverse proxy or load balancer then make sure that it is either forwarding the originating IP addresses or that you activate the second and third scanning options in my plugin, otherwise you may not be able to block anyone.

Finally, if you find this plugin useful, consider providing a link back to my site so that others can find it.

UPDATE (2/12/2008): Updated to 1.01 to handle the changed line endings in the CSV.

Download the "Block Countries" WordPress Plugin


66 Responses to “WordPress Plugin: “Block Countries””
  1. Joe says:

    Does anyone know of a "Ban or Block" plugin that integrates with Akismet? Now that would be the ideal plugin, then you can Ban only the spammer. Akismet does a good job catching spam but only gives the option to approve or delete, why not delete and Ban together?

  2. Antivirus says:

    Interesting plug-in. Would be really useful for me at times. Certainly is annoying at times. anyway Thanks again. Keep it up. Cheers

  3. I have been looking for this plugin for years. I am happy that you created it Jason. I will be sure to tell the rest of the world about it on my blog.

    Thanks again!

  4. Privacy Protection says:

    I've found a free anonymous browsing proxy here http://www.surfshuffle.com, thought I'd share with whoever is interested before it gets blocked

  5. amenodimeno says:

    That's good man, keep it going.

  6. tvbaddict says:

    thank you for the plugin...

  7. mike says:

    the plugin is great! i've searching for this a lot. I just would like to know if it's possible to design the "forbidden" page.
    There is some screenshot of what the blocked countries see?}


  8. Jason says:

    The blocked user sees a simple text message and receives a 403 forbidden http response. You could easily edit the plugin to do something else though.

  9. Problems with installation says:

    I get this message in the admin:
    An error occurred while processing the CSV file.

    I have permissions to write in my directories. Do you know how to get this to work?

  10. GT says:

    I get the message "An error occurred while processing the CSV file.", even with FILE permission granted to the MySQL user. I am assuming that you tested this only on a server that was acting both as a web server (where the files go) and a MySQL server. On my setup I have separated servers for web and MySQL thus your plugin simply can't import the CVS file, as the FILE permission is only valid inside the MySQL server.

    The solution was to manually upload the csv file to my MySQL server at its /root directory and running the following command on mysql command line:

    LOAD DATA LOCAL INFILE '/root/ip-to-country.csv' INTO TABLE `wp_iptocountry` FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r';

  11. Galaxy says:

    Can you write a plugin for blocking on certain posts ( tagged by post_custom_stuff or just a tag of the post ) ?

    Well, for certain countries, websites that contain certain words will get blocked. I would like to make that post can be viewed by those who have a foreign SSL proxy but not those who come directiy from that country. Since those came directly are monitored by the blocker softwares in the routers.

  12. Sherry says:

    I installed the plugin and added the .csv file with no problem, but whenever I try to block one country it seems to block across the board as I am not able to get to my site or WP Admin anymore. Note: I did NOT block my own country (USA) but only blocked AUS via Remote_Addr. When I do a query, the bc_1enabled is set to True and the bc_codes is set to AUS.

    I would expect that it should work, but I'm getting a blank screen when I hit my site. Note: I'm not actually seeing the 403 message that is coded into the .php script, so I assume it's choking somewhere else.


  13. iPhone 5 says:

    It dose not work here it say: An error occurred while processing the CSV file. please help me. thank you

  14. Roman says:

    Cool! This is the plugin that I was looking for. Thanks!


Check out what others are saying about this post...
  1. Delicious Bookmarks for July 2nd through July 3rd « Lâmôlabs says:

    [...] WordPress Plugin: “Block Countries” | Utter Ramblings – July 3rd ( tags: wp wordpress plugin block countries extension ) [...]

This site is no longer updated. If you have a need for RHEL/CentOS LAMP Stack updates outside the normal channels, I recommend ART. https://updates.atomicorp.com/channels/