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. safari says:

    don't know why you block Vietnam???

  2. testing says:

    I'm trying this and get the following error when trying to load the csv:
    LOAD DATA LOCAL INFILE '/home/.../public_html/val/wp-content/plugins/block-countries/ip-to-country.csv' INTO TABLE `wp_iptocountry` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY ' ';

    Just wondering if maybe the csv was corrupted in download since it's big and I'm on a dialup....or if you have any ideas.


  3. Ozh says:

    Blocking a country... This has to be the dumbest idea of the month.

    Not only it's totally vain since it's easy as pie to use a public anonymous or less public open proxy to defeat IP based filtering, but it's also uber-jerkish to want to block an entire country when a single jerk annoys you.

    Jeez, people sometimes...

  4. Jason says:

    If you don't like it, don't install it.

    As to blocking an entire country to get rid of one user, I don't consider that a problem. This person was stealing content from my site and passing it off as his own. On top of that, he was hot-linking files from my server.

  5. Jason says:


    That doesn't seem like the entire error. What was the rest of the message? Did it tell you why it failed on that command? Also, were those "..." part of the message or did you put those there to censor the post?

    It could be that the download was corrupt (although if the zip file extracted it probably wasn't) but it's more likely that your MySQL user account doesn't have the "FILE" permission which is required for the "LOAD DATA LOCAL INFILE" statement.

  6. Dave says:

    I think having the option to block countries is a good idea for those that want to protect their data, not to mention this was the type of thing that was done just to say it could be done and Jason was the one to do it. Good Job Jason!

  7. Jason says:

    Thanks Dave.

  8. Tia O'Connor says:


  9. Ante says:

    Had the same problem as "testing" above (19th April), i.e. I didn't have "FiLE" permission. However I solved it by entering phpmyadmin and manually import the .csv-file. After some trial and error I had to change from the default ";" to "," (without quotes) as field divider. Now everything works fine. Thanks Jason!

  10. Jason says:


    I'm glad to hear that you got it working.

  11. mark says:

    Great plugin! I modified it to allow a login page to override it for someone in a blocked country. Basically, I have a website for another country and I need the country I'm in to be blocked. Well, naturally that blocked me as well.

    So with a bit of modification and a new seperate login page, I now go to the login page and put in my username and password, it then creates a cookie which my modifications check for to see if I am allowed in the website despite being in the blocked country.

    A simple adjustment allows for the expiration time of the cookie to be changed.

    Let me know if you would like my modifications, I can either email them to you, or I could post the simple instructions on the comments if you would like.

  12. Jason says:


    Sure, send over the changes you've made and I'll see about integrating them. Use the comment form on my "About" page.


  13. Rod says:

    Can't wait to get this to work as someone is stealing my content as well. Unfortunately, only one country is showing up in the list (United Kingdom). Any idea as to what I'm doing wrong?

    Thanks for taking the time to make this plug in!

  14. Jason says:


    It's possible that that the newest version of their DB has changed formats slightly. I'll take a look. If it has, I'll post a link to the version I have and then update my code to work with their newer DB.

  15. Rod says:

    Thanks Jason!

  16. Jason says:


    I've updated the zip file. The code now works with the 2/3/2008 database (and I would assume, future updates).

  17. mark says:

    i updating both files, but i only see united kingdom too.

  18. Rod says:

    Yeah - I figured I was just doing something wrong and was going to try a re-install - because I still have the same problem.

  19. ed seward says:

    What do you mean when you state "it's more likely that your MySQL user account doesn't have the "FILE" permission which is required for the "LOAD DATA LOCAL INFILE" statement."

    I have the MySQL user permissions set to "dba". Does this take care of this problem?

    I get the "An error occurred while processing the CSV file." error message when I attempt to use the update function.



  20. JC says:

    My website provide services in my city, locally. The other day I noticed in my logs that a third party WP plugin was being exploited. Most of the IP ranges were from outside the U.S. Since my website caters to the U.S. only, I had no problem blocking other countries. Thanks for the plugin.

  21. Jason says:


    There is no 'dba' permission in MySQL. Are you sure you're not using MSSQL or something else?

    @mark & Rod,

    What version does your plugin's page say? It should be 1.01. If that is the case, download the IP-to-Country database, place it in the plugin folder, check the little box on the Plugin properties page, and then hit "Update".

  22. Rod says:

    Hey Jason,

    Still no luck - and it says it's 1.01. Maybe I'm doing something else wrong - but it still only shows the United Kingdom.

  23. Jason says:


    You're using the 2/4/2008 database and you've told it to reload? I just tested it again and it worked for me so there's really no reason why running the update shouldn't fix it for you.

    About the only thing I can think of is to try removing the plugin and database table and then reinstalling the plugin.

  24. Xem Phim Online says:

    I think it is a good ideal, but i am not sure if it "actualy work" since those jerks can still use Sock Proxy to get in, and you are some what limit legits people to visit your sites.

    Just my 2cents

  25. Jason says:


    I'm not real familiar with SOCKS proxies so that may be true.

  26. Dr. Lead Based Pain says:

    Block Countries doesn't seem to work with WordPress 2.5.

    Any plans for update?

  27. circuits says:

    I think having the option to block countries is a good idea for those that want to protect their data, not to mention this was the type of thing that was done just to say it could be done and Jason was the one to do it. Good Job Jason!

  28. Jason says:

    I haven't tested my plugins (or my site) with WP 2.5 yet but plan to over the next couple days. Once I do, I'll post an update.

    So I know going in, what errors, if any, are you receiving under WordPress 2.5?

  29. Klaids says:

    In WordPress 2.5.1 Block Countries Options - only one country is possible to select and it`s Great Britain 🙂 How to change country and select multiple?

  30. April says:

    This is very strange. I have never thought about installing any kind of plugin like this. However I have had people hotlink images without permission but these were people in Western countries adding my photos to their myspace pages etc. They don't seem to realize how anoying that it!

  31. Lisa says:

    I too am installing this and getting UK showing up. I installed the plug-in, installed the csv. file into that, went to settings and activated everything. No luck. Do I need to add this csv to SQL or anything?

  32. marketing says:

    i'm really please i found this website. I have learned a lot by reading here. keep up good work.

  33. AlexCuba says:

    this is certainly pleased

  34. picture fortune wheel slot of says:

    machines wheel of slot fortune of wheel slot fortune download

  35. Visas Australia says:

    Awesome, just what I wanted. Some scum bags are stealing my content as well. Nice plug-in. This would do the trick on the theft. Thanks for the stuff. Keep up the awesome work.

  36. Beth @ Casual Relationships says:

    Great, I needed this one. Had few troubles with my earlier blog and I had to move to a new one. And this is one plug-in I really needed. Had a hard time with the earlier blog. Too bad I didn’t come across this earlier. It would have saved me so much of time and money. Sigh! Oh well, I have it now. Thanks a lot

  37. Nice plug-in. I was looking for something similar. I had this problem with few people spamming my site from few countries. Didn't know how to stop it until now 🙂 thanks or the useful plug-in. Keep it up. You rock man.Cheers

  38. UK Online says:

    Neat. that's one awesome plug-in. I was looking for a similar plug-in for my personal blog. Spamming sucks. And manually deleting them sucks more. Anyway, thanks again. Cheers

  39. Fluke Ti10 Thermal Imager says:

    Nice post. Spam comments are a headache. This plugin will help to reduce them. Very useful. Thanks for sharing the plugin with us. Keep up the good work.

  40. Interesting plug-in. Well it's somewhat not fair to use such plug-in and block the whole country because of few certain people. but there are sometimes you just need to do it. This would be needed for a similar instance.

  41. That's a really useful plug-in. At times there is not other alternative other than to ban the whole country from the blog. This is a really good plug-in for that use.

  42. anon says:

    well i would block romainia because most of the spam comments comes from that country.

    also most spam comes from small countries.

    Reasons why I would use this.


Check out what others are saying about this post...
  1. [...] Block Countries uses the IP2Country database to easily enable you to block certain countries from your blog. [...]

  2. [...] ダウンロード:My new WordPress plugin: “Block Countries” at Utter Ramblings 導入&使い方: [...]

  3. New Wordpress theme & plugin releases. Something ‘fresh’ | RandLIFE says:

    [...] Block Countries uses the IP2Country database to easily enable you to block certain countries from your blog. [...]

  4. Blogtology » Blog Archive » WP Plugs April 21, 2007 says:

    [...] Block Countries uses the IP2Country database to easily enable you to block certain countries from your blog. [...]

  5. WeblogToolsCollection Español » Blog Archive » Plugins de WordPress publicados 18/4 says:

    [...] Block Countries usa la base de datos de IP2Country  para permitir el facil bloqueo de tu blog a ciertos países. [...]

  6. Listado de plugins del dia at Escuela De Blogs says:

    [...] Block Countries usa la base de datos de IP2Country para permitir el facil bloqueo de tu blog a ciertos países. [...]

  7. Wordpress spraudnis “Block Countries” | Klaid`s blog says:

    [...] :), kas saucas WordPress spraudnis “Block Countries”. Par to viss smalki ir aprakst?ts autora m?jas lap?. Diemž?l pagaid?m izskat?s, ka izmantojot versiju 2.5.1 šis spraudnis ?sti nestr?d? k? [...]

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/