Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ocReaper/cfdc3efaaa91c4de6319dc7e5d4b03e2 to your computer and use it in GitHub Desktop.
Save ocReaper/cfdc3efaaa91c4de6319dc7e5d4b03e2 to your computer and use it in GitHub Desktop.
Deactivate GDPR WordPress plugin for non EU IP addresses
<?php
/**
* To have a list of two letter country codes of the european countries
*
* @var string[]
*/
const EUROPEAN_ALPHA_TWO_COUNTRY_CODES = [
"IE",
"GB",
"PT",
"ES",
"FR",
"IT",
"MT",
"GR",
"BG",
"RO",
"HU",
"HR",
"SI",
"AT",
"SK",
"CZ",
"PL",
"DE",
"LU",
"BE",
"NL",
"DK",
"SE",
"FI",
"EE",
"LV",
"LT",
"IC",
"MQ",
"RE",
"GF",
"CY"
];
/**
* To obtain the client's IPv4 IP address
*
* @return string
*/
function get_client_ip_address() {
$ipAddress = '';
if ( $_SERVER['HTTP_CLIENT_IP'] ) {
$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
} else if ( $_SERVER['HTTP_X_FORWARDED_FOR'] ) {
$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if ( $_SERVER['HTTP_X_FORWARDED'] ) {
$ipAddress = $_SERVER['HTTP_X_FORWARDED'];
} else if ( $_SERVER['HTTP_FORWARDED_FOR'] ) {
$ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if ( $_SERVER['HTTP_FORWARDED'] ) {
$ipAddress = $_SERVER['HTTP_FORWARDED'];
} else if ( $_SERVER['REMOTE_ADDR'] ) {
$ipAddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipAddress = 'UNKNOWN';
}
return $ipAddress;
}
/**
* To remove GDPR plugin from the list of the active plugins
*
* @param string[] $plugins
*
* @return string[]
*/
function virtually_deactivate_gdpr_plugin( $plugins ) {
if ( in_array( 'gdpr/gdpr.php', $plugins ) ) {
unset( $plugins[ array_search( 'gdpr/gdpr.php', $plugins ) ] );
}
return array_values( $plugins );
}
// first get the client's ip address
$clientIp = get_client_ip_address();
// skip the process if the IP is unavailable or not valid
if( ! filter_var($clientIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) {
return;
}
// get the two letter country code of the client by passing it's ip to a free webservice
$result = \Requests::get( 'https://ipinfo.io/' . $clientIp . '/country' );
$response = trim( $result->body );
// skip the process if the client is not in a european country
if ( in_array( $response, EUROPEAN_ALPHA_TWO_COUNTRY_CODES ) ) {
return;
}
// finally virtually deactivate the gdpr plugin
\add_filter( 'option_active_plugins', 'virtually_deactivate_gdpr_plugin' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment