WordPress: CloudFlare-Country-Login

This snippet allows us to tap into the CF_IPCountry [1] header and determine if this country is allowed to login. We put allowed countries into the array situated in the cf_country_login() function.
Put this snippet into your theme’s functions.php and you’re all done, but don’t forget to first edit $urel and the default array of allowed countries.

// Allow only logins from specified countries (requires CloudFlare IP Geolocation to be enabled)
add_action( ‘wp_authenticate’, ‘cf_country_login’, 1);
function cf_country_login()
if ( !isset($_SERVER["HTTP_CF_IPCOUNTRY"]) )

if ( !in_array($_SERVER["HTTP_CF_IPCOUNTRY"], array(‘DK’)) )
$urel = ‘http://k0nsl.org’;
echo ‘Sorry, but you cannot login at this time.’;
header("Refresh: 5; url=’.$urlel.’");

Available also @ GitHub:


1. What does CloudFlare IP Geolocation do?


Add a Comment

Your email address will not be published. Required fields are marked *