Skip to content

Instantly share code, notes, and snippets.

@timersys
timersys / functions.php
Last active Sep 23, 2021
Custom country widget for GeotargetingWP
View functions.php
<?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' );
@timersys
timersys / functions.php
Created Jul 9, 2021
Set custom data based on urls on GeotargetingWP
View functions.php
<?php
class GeotWP_Url_Location {
public function __construct() {
add_filter( 'geot/cancel_query', [ $this, 'set_custom_data' ] );
}
/**
* Check if url is set and modify data
@timersys
timersys / functions.php
Created May 27, 2021
Change the GeotargetingWP translation file location
View functions.php
<?php
function wpse159536_override_mofile_path( $mofile, $domain ){
if( 'geot' == $domain ){
$mofile = WP_LANG_DIR . '/plugins/' . basename( $mofile );
}
return $mofile;
}
add_filter( 'load_textdomain_mofile', 'wpse159536_override_mofile_path', 10, 2);
@timersys
timersys / functions.php
Created May 26, 2021
Changing location in GeotargetingWP using URL parameters or querys tring
View functions.php
<?php
/**
* Allow users to change location with query string like this
* https://yourdomain.com?geot_country=US
**/
add_action( 'init', 'change_geolocation_with_url' );
function change_geolocation_with_url(){
if( ! isset( $_GET['geot_country'] ) ) {
return;
}
@timersys
timersys / app.js
Last active May 10, 2021
Access Javascript geo info in GeotargetingWP
View app.js
$(document).on('geotwp_ajax_success', function(e, data) {
if( typeof data.geo != 'undefined') {
// create a custom cookie with zip value and 999 days duration
GeotCreateCookie('geot_switcher', data.geo.city.data.zip, 999);
// Add a marker in a custom google map
if( data.geo.geolocation.data.latitude && data.geo.geolocation.data.longitude && jQuery('#geot-map').length != 0 ) {
var stanton = {lat: parseFloat(data.geo.geolocation.data.latitude), lng: parseFloat(data.geo.geolocation.data.longitude)};
add_marker(stanton);
}
@timersys
timersys / file.php
Created Apr 23, 2021
AJAX PHP functions in GeotargetingWP
View file.php
<?php
/**
* Practical example on how to use ajax php functions
**/
$html = '<p>This is your HTML you want to apply geotargeting, if you have complex and large content you could use ob_start() and ob_get_clean() to fill this variable with the HTML</p>';
$html_world = '<p>Some other HTML for everyone except US and CA</p>';
// We echo the part for and US,CA. Javascript will take care of hide or show the $html
echo ajax_geotwp_filter( ['country' => 'US,CA'],$html);
View functions.php
<?php
// geot_user_country
// Return object of current user country details
$country = geot_user_country();
echo $country->name;
echo $country->isoCode;
//geot_country_code
// return current user country code
@timersys
timersys / functions.php
Created Jan 19, 2021
Wrap functions to be safe
View functions.php
<?php
if( function_exists( 'geot_target' ) ) {
if( geot_target( 'AR' ) ) {
echo 'Only show to Argentina';
}
}
@timersys
timersys / class-geot-dropdown-widget.php
Created Aug 26, 2020
Custom ZIP switcher for GeotargetingWP
View class-geot-dropdown-widget.php
<?php
/**
* Adds GeoTarget Widget
* @since 1.0.0
*/
class GeotS_Widget extends WP_Widget {
/**
* Register widget with WordPress.
@timersys
timersys / geot-wprocket.php
Created Apr 13, 2020
Tiny plugin to enable State cache to Wp Rocket + GeotargetingWP
View geot-wprocket.php
<?php
/**
* Plugin Name: Wp Rocket + GeotargetingWP States
* Plugin URI: https://geotargetingwp.com
* Description: Enable states cache for WPRocket.
* Author: timersys
* Author URI: https://geotargetingwp.com
*/
/**