Documentation is a work in progress.
Create a file named “use-my-geolocation.inc” containing your custom cca_custom_lookup function and place it in your “/wp-content/cca_maxmind_data/” directory.
As a minimum the function must:
- set $GLOBALS[‘CCA_ISO_CODE’] to the 2 character ISO country code identified by your geolocation system; if your geolocation system reports an error or cannot identify the visitor location the variable should either be set as an empty “” string or with the ISO code for the country you wish to use by default.
- return a string. You should use this to to identify success or failure of your geolocation look-up. (this message is displayed on the CCA Settings “Diagnostics” tab).
Example 1: connection file for Amazon Web Services Cloudfront (not tested)
<?php
function cca_custom_lookup() {
$GLOBALS['CCA_ISO_CODE'] = '';
if (! isset($_SERVER['CloudFront-Viewer-Country']) ) {
return 'Error: AWS Cloudfront is not providing country';}
// check if it is a 'valid' country code
if (ctype_alnum($_SERVER['CloudFront-Viewer-Country'])
&& strlen($_SERVER['CloudFront-Viewer-Country']) == 2) {
$GLOBALS['CCA_ISO_CODE'] = $_SERVER['CloudFront-Viewer-Country'];
return 'AWS Cloudfront geolocation appears to be working';
}
return 'Error: CFront country set to' . esc_html($_SERVER['CloudFront-Viewer-Country']);
}
?>
You can also provide additional information to the diagnostics tab by appending additional information to string $GLOBALS[‘cca-lookup-msg’] (see example 2).
“Fatal errors” are – fatal. You will have to ensure your PHP is valid, and cater for any potential errors such as class or function “already exists”.
If you are using the Country Caching script for WPSC: it will probably be executed before other plugin functions (and parts of WordPress) are “loaded”. Attempting to use these functions within your custom function may cause fatal errors (setting WPSC to “late init” may solve this but result in a slight loss of speed). You will have to use “pure PHP” to execute your geolocation system look-ups.
Example2: use Cloudflare BUT convert all “XX” (unknown) locations to “EU” for GDPR law requirements (requested by CCA plugin user)
<?php
function cca_custom_lookup() {
if ( isset($_SERVER["HTTP_CF_IPCOUNTRY"])) { // Cloudflare OK
$GLOBALS['cca-lookup-msg'] .= '<br>Using Cloudflare for country id.';
$GLOBALS['CCA_ISO_CODE'] = strtoupper($_SERVER["HTTP_CF_IPCOUNTRY"]);
if ($GLOBALS["CCA_ISO_CODE"] != 'XX' && ctype_alnum($GLOBALS["CCA_ISO_CODE"])
&& strlen($GLOBALS["CCA_ISO_CODE"]) == 2) {
return 'Custom geolocation appears to be working';
}
$GLOBALS["CCA_ISO_CODE"] = 'EU';
$GLOBALS['cca-lookup-msg'] .= 'CF Country Code invalid. Treating as "EU"';
return 'Unable to identify country';
}
$GLOBALS["CCA_ISO_CODE"] = 'EU';
$GLOBALS['cca-lookup-msg'] .= '<br>You are NOT using Cloudflare.';
return 'Error: custom look-up failed';
}
?>
Leave a Reply