Skip to content

Instantly share code, notes, and snippets.

@webtoffee-git
Last active September 29, 2025 11:24
Show Gist options
  • Select an option

  • Save webtoffee-git/6d92b87f6aba84e6eb6416c0d6453ace to your computer and use it in GitHub Desktop.

Select an option

Save webtoffee-git/6d92b87f6aba84e6eb6416c0d6453ace to your computer and use it in GitHub Desktop.
Geo-Target GDPR Cookie Banner - By WebToffee
<?php //Do not copy this line of code
/**
* Add custom cookie consent handler for US visitors
*/
function wcc_custom_cookie_consent_us_handler() {
?>
<script type="text/javascript">
(function() {
// Wait for the cookie consent store to be initialized
const waitForWccStore = setInterval(() => {
if (typeof window._wccConfig !== 'undefined' && typeof window.webtoffee !== 'undefined') {
clearInterval(waitForWccStore);
handleUSVisitors();
}
}, 100);
async function handleUSVisitors() {
try {
// Get visitor data from the API
const response = await fetch(window._wccConfig._restApiUrl);
const visitorData = await response.json();
// Check if visitor is from US
if (visitorData && visitorData.country === 'US') {
const ref = window.webtoffee;
// Set all cookie categories to yes
window._wccConfig._categories.forEach(category => {
ref._wccSetInStore(category.slug, "yes");
});
// Unblock scripts
if (typeof _wccUnblock === 'function') {
_wccUnblock();
}
}
} catch (error) {
console.error('Error in US cookie consent handler:', error);
}
}
})();
</script>
<?php
}
add_action('wp_footer', 'wcc_custom_cookie_consent_us_handler', 99);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment