This Cloudflare Worker sets the tu-geoip-hide cookie based on visitor geolocation to prevent unnecessary admin-ajax calls on every page load.
This setup is advanced and is appropriate for sites that already use Cloudflare for page caching and see traffic spikes of 100s or 1000s of visitors in short timeframes. If you're using Cloudflare caching but have the Termageddon plugin setting "Enable page caching support via AJAX" turned on - you'll see in your logs that each visitor is still generating an admin-ajax call to get the location data. This setup resolves that issue by using Cloudflare Workers instead of your own server resources to do that check.
The complexity of setting up and maintaining this system would be overkill for most low traffic sites.
Also, if you're not using any Page caching at all, you don't need this - just disable the AJAX toggle in the plugin geolocation settings instead.
- First visit: Worker detects no cookie → Sets
tu-geoip-hidecookie based on location - Subsequent visits: Cookie exists → Worker does nothing, plugin uses cached value
- Log in to your Cloudflare Dashboard
- Go to Workers & Pages
- Click Create application → Create Worker
- Click Start with Hello World
- Give it a name like
termageddon-geolocation(or use the auto-generated name) - Click Deploy (this deploys the default "Hello World" worker)
- After deployment, click Edit Code (top right)
- In the code editor, select all the default code and delete it
- Copy and paste the entire contents of
cloudflare-worker.jsinto the editor - Edit the values at the top to match your site's show/hide settings. You can come back here and edit these anytime. Note that the plugin's toggles will NOT update these anymore. You need to set them here if you're going to use this system.
- Click Save and Deploy
- In Cloudflare Dashboard, go to Websites → Select your domain
- Go to Workers Routes
- Click Add Route
- Route:
example.com/*(or*.example.com/*for all subdomains) - Worker: Select the worker you created above
- Be sure to select fail mode: Fail open (proceed) - this will allow the system to fall back to the normal server-side admin-ajax.php system if you hit your 100k daily limit
- Click Save
Performance note:
- The worker checks URL extensions and skips static files before fetching from origin, saving bandwidth
- However, all matching requests still count toward your 100,000/day worker invocation quota
- For most sites, this is more than sufficient
- If needed, you can add multiple specific routes (e.g.,
example.com/with no trailing wildcard,example.com/article/*,example.com/product/*) instead of/*to reduce invocations further
- Geolocation (top toggle)
ON - Note that the region selection toggles no longer do anything unless Cloudflare is bypassed! You must set these the way you need these here and then repeat that exactly in the configuration at the top of
cloudflare-worker.js - Enable Location Logging must be
OFF. If it is on, admin-ajax will always fire for debugging reasons. - Enable page caching support via AJAX must be
ON. This is needed as a fallback and for the existing cookie check to work as expected.
Visit your site in an incognito/private window:
- Open Developer Tools → Network tab
- Load a page and check the response headers. Look for:
Set-Cookie: tu-geoip-hide=true(orfalse) - You should NOT see any calls to admin-ajax with payload
uc_geolocation_lookup - Open Developer Tools → Application tab → Cookies → https://example.com
- You should see the
tu-geoip=hidecookie with the correct true/false value.
On staging/development environments (domains containing staging., stg., .local, or .localhost), the worker automatically adds debug headers:
X-TU-Location: Shows detected location (e.g.,US-CA-San Francisco)X-TU-Hide: Shows if consent banner is hidden (trueorfalse)
Test different locations using the ?termageddon-usercentrics-debug parameter:
https://staging.example.com/?termageddon-usercentrics-debug=california
https://staging.example.com/?termageddon-usercentrics-debug=eu
https://staging.example.com/?termageddon-usercentrics-debug=canada
Available test locations:
california, colorado, connecticut, delaware, florida, indiana, montana, oregon, texas, utah, virginia, newyork, eu, uk, canada
When using the debug parameter:
- An additional
X-TU-Debug-Locationheader shows which test location is active - Debug headers are enabled regardless of domain
- The cookie is set based on the test location instead of actual geolocation
Debug headers are automatically disabled in production. They only appear when:
- Domain contains
staging.,stg.,.local, or.localhost, OR - URL includes
?termageddon-usercentrics-debugparameter
Edit the SHOW_CONSENT_BANNER_IN configuration at the top of the worker to customize which locations should show/hide the consent banner:
- Name:
tu-geoip-hide - Value:
"true"(hide) or"false"(show) - Expiration: Session (until browser closes)
- Path:
/ - SameSite:
Lax - Secure: Yes (HTTPS only)
- Before: 1000s of unique visitors = 1000s of admin-ajax calls = high server load
- After: 1000s of unique visitors = 1000s of fully cached responses = minimal impact
- Worker overhead: <1ms per request
- Basic geolocation (
countryandregion/state) is available on all Cloudflare plans including Free. - Workers are available on all Cloudflare plans (Free, Pro, Business, Enterprise)
- Free and Pro plans includes 100,000 requests/day
- This worker adds ~0.5-1ms latency per request
- No changes needed to the WordPress plugin code