Skip to content

Instantly share code, notes, and snippets.

@timersys
Last active September 23, 2021 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timersys/3a3d74bf42fb4fade6afc194605b7210 to your computer and use it in GitHub Desktop.
Save timersys/3a3d74bf42fb4fade6afc194605b7210 to your computer and use it in GitHub Desktop.
Custom country widget for GeotargetingWP
<?php
/*
* The following code serves as an example and could be copied over to your theme's functions.php file
* Ideally you want to separate your javascript files and load them separatly.
* This example it's based on the following HTML structure:
* <a href="#" data-iso="US" class="geot-countries">United States</a> - <a href="#" data-iso="AR" class="geot-countries">Argentina</a>
*/
add_action('wp_footer', 'my_custom_geot_widget' );
function my_custom_geot_widget(){
?>
<script>
jQuery(function ($) {
$(document).ready(function () {
$('.geot-countries').on( 'click', function(e) {
e.preventDefault();
const iso = $(this).data('iso');
// cookie for the plugin and our regular dropdown widget
geotWP.createCookie('geot_country', iso, 999);
// if you use wprocket add this cookie to create different cache based on countries
// geotWP.createCookie('geot_rocket_country', iso, 999);
// if you are in pantheon hosting, you need to add this line
// geotWP.createCookie('STYXKEY_geot_country', iso, 999);
// select the option to apply your own css
$('.geot-countries').removeClass('selected');
$(this).addClass('selected');
// reload the page, so the change applies
window.location.reload();
});
});
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment