Skip to content
Rezha Julio

wordpress

I built my own country blocking instead of paying for Wordfence Premium

8 min read

Country blocking is a Wordfence Premium feature. I only wanted that one, so I wrote a small mu-plugin. Then WP-Optimize's page cache showed me why the homepage kept loading anyway.

A stone border gatehouse stops some travelers at a barrier while others slip past along a path of stacked boxes
A stone border gatehouse stops some travelers at a barrier while others slip past along a path of stacked boxes

I run Wordfence free on my WordPress site. One feature kept nagging me: country blocking. It sits behind Premium, which is $149 a year, and I did not want to pay for a whole plan just for that.

So I did what I usually do first. I downloaded the plugin and read it.

What Wordfence actually gates

Wordfence 9.0.2 is GPLv3, one zip for free and paid users, no obfuscation. Your license is a hex API key. The plugin sends it to noc1.wordfence.com on almost every API call, and the server sends back whether the key is paid, what tier it is, and how many days are left. The client caches those answers in its own database table.

Country blocking lives in that client code. The block creation code has no paid check. The enforcement code does: if the key is not paid, a country block returns no match. That is the whole gate for this feature.

Other premium features are different. The real time threat feed, the malware signatures, and the real time IP blocklist are not in the zip in usable form for a free key. The server only serves them to a paid, site bound key. Flipping a local flag would not conjure those feeds, and the next API sync would overwrite the flag anyway.

That distinction decided the shape of the work. I wrote a feature that does the same job, on its own, with its own settings and its own block list.

The plugin I built instead

It is a single mu-plugin file, country-blocking.php, in wp-content/mu-plugins/. Must use plugins load automatically and cannot be turned off from wp-admin, which is what I wanted for a security control.

It runs on plugins_loaded at priority 1. It looks up the visitor’s country, checks it against my list, and sends a 403 if there is a match. The settings page lives under Settings, Country Blocking. I set it to CN, RU, block mode, whole site plus wp-login.php and xmlrpc.php.

For GeoIP it reuses Wordfence’s own reader at runtime, reading Wordfence’s bundled lib/geoip.mmdb. It copies no Wordfence code and no database file. If no database is found, or a lookup fails, it fails open and blocks nobody. I would rather fail open than take the site down because a file moved.

Administrators are never blocked, and logged in users can be exempted too. That matters for testing, and it also matters for not locking myself out. Every blocked attempt goes into wp_options, plus the PHP error log, so I can see it without hunting log files. Retention is configurable, up to 365 days.

I installed it, checked the settings page diagnostic first (database found, my own IP resolved to SG), tested in log only mode, then switched to block mode.

Login blocking worked right away. The homepage did not.

The test that did not make sense

Logged out, different browser, through a CN VPN. The login page blocked. The homepage loaded. One post blocked. Another post loaded.

My first guess was the logged in bypass. That was wrong, because the test browser was logged out. My second guess was a settings mistake. That was also wrong. Whole site blocking was ticked.

The log settled it. In about five minutes, my own test requests through a CN VPN produced 19 blocked entries: wp-login.php several times, /tulisan/dua-bahasa-dua-kepala/ several times, even /favicon.ico. There was no entry for the homepage, and no entry for the post that loaded. No IP address is published here.

The plugin only logs attempts it actually sees. If it had seen the homepage request from a blocked country, it would have logged it and returned 403. No entry plus a loaded page means the plugin never ran for that request.

Why the homepage loaded anyway

WP-Optimize page cache. It caches each URL separately, and it serves a cached page before WordPress loads any plugin.

WordPress loads wp-content/advanced-cache.php before mu-plugins, if page caching is on. WP-Optimize’s version of that file checks its cache, and on a hit it sends the file and exits. My mu-plugin, and every other regular plugin, never gets a chance to run.

Use the switches below to follow one request. Pick a blocked visitor and an already cached page, and that is my homepage: the cache answers first, so the country check never runs. Move the block in front of the cache and the problem goes away.

Visitor
Page
Block runs

    The 19 log entries and the missing homepage entry are from my test in the post. The three switches model the behavior I describe, they do not replay real traffic.

    The homepage and the post that loaded were already cached, probably before I enabled blocking, or after an allowed visitor loaded them. The post that blocked was not cached at that moment, so WordPress ran, the plugin ran, and it got a 403.

    Purging the cache would only prove the point for a few minutes. The next allowed visitor would rebuild the cache, and the next blocked visitor would get the cached copy again. A WordPress level block cannot stop a request that never reaches WordPress.

    Wordfence handles this differently, and that is worth knowing. In Extended Protection mode, Wordfence sets PHP’s auto_prepend_file to its WAF bootstrap. That file runs before any script, before advanced-cache.php, before WordPress. Its firewall checks run first, cache second. In Basic Protection mode, Wordfence loads as a normal plugin, after the cache, and it has the same problem my mu-plugin has.

    Even in Extended mode, Wordfence’s country block would not have helped me. Its WAF code only enforces country blocks when the key is paid, and mine is free. Its IP blocks and firewall rules would run before the cache, but the country block would not.

    I looked at two durable fixes. One was a wrapper around advanced-cache.php that checks country before WP-Optimize serves a cached page. I built a clean room version of that locally, using the same GeoIP reader, and tested it against the real database. It works, but WP-Optimize owns advanced-cache.php and rewrites it on plugin updates and site URL changes, so the wrapper would need looking after. The other was blocking at Caddy, before PHP and cache ever run. That is the clean version, if I ever decide I need it.

    For now I decided I do not need it. Login blocking is the part that matters most, and wp-login.php is never cached, so it blocks reliably. The homepage and posts are public content anyway. A blocked visitor reading a cached public post is not a security hole, it is just a public post loading as usual. If you need a block that no cached page can bypass, that is a job for the edge, before the cache. I cannot tell you whether that meets any legal or licensing requirement you have.

    I am not a lawyer, and this is not legal advice. Here is how I reasoned about it, in plain terms.

    The plugin I built is independent: my own code, my own settings, no Wordfence API calls, no Premium claim. It implements a generic idea, block by country from an IP address. Building a competing feature is ordinary WordPress plugin work.

    Flipping Wordfence’s paid flag to use its own country blocking without paying would be a different thing. That would be using a paid entitlement I do not have, under a license and service terms I did not buy into. I did not do that, and I would not publish a post telling people to do that.

    If you publish a plugin like this, do not bundle Wordfence’s GeoIP database with it. Read it at runtime from a Wordfence install on the same site, or tell users to supply their own database. That keeps the boundary clean: your code is yours, their data stays theirs.

    I kept the settings as they are. Block mode, CN, RU, whole site plus login ticked. Uncached pages block, cached pages load, login always blocks. That is the trade I accepted. I am writing it down so future me does not have to rediscover why the homepage behaves this way.

    Get the plugin

    The plugin file is on a GitHub gist: country-blocking.php.

    Download it and upload it to wp-content/mu-plugins/. You cannot upload a mu-plugin from wp-admin, so use a file manager or SFTP. It ships disabled with an empty block list, so test in log only mode first, then switch to block mode. It needs a GeoIP database: it reads Wordfence’s bundled database at runtime if Wordfence is installed, or a database file you supply yourself. It does not bundle one.


    Related Posts


    Previous Post
    Building a Telegram bot to babysit 24,000 developers