WordPress Plugin: “Block Countries”
April 16, 2007 by Jason · 62 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.
That's good man, keep it going.
thank you for the plugin...
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?}
thanks.
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.
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?