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

Comments

66 Responses to “WordPress Plugin: “Block Countries””
  1. 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.

  2. 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?

  3. 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';

  4. 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.

  5. 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.

    Help???

  6. iPhone 5 says:

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

  7. Roman says:

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

Trackbacks

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? [...]

  8. 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 ) [...]



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

 

Note: This post is over 5 years old. You may want to check later in this blog to see if there is new information relevant to your comment.

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. Use of a non-personal web site or blog in the "Web Site" field and/or leaving a comment that is off-topic or inappropriate may result in the comment being edited or removed at the discretion of the site owner.