Skip to content

Instantly share code, notes, and snippets.

@timersys
Created April 23, 2021 16:33
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/16701ec1afd0ea6fa1c636f3846a16dc to your computer and use it in GitHub Desktop.
Save timersys/16701ec1afd0ea6fa1c636f3846a16dc to your computer and use it in GitHub Desktop.
AJAX PHP functions in GeotargetingWP
<?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);
// We echo the part for the rest of the world
echo ajax_geotwp_filter( ['exclude_country' => 'US,CA'],$html_world);
// another way to do this with php
echo do_shortcode('[geot_filter country="US,CA"]'. $html . '[/geot_filter]');
/**
* FUNCTION USED IN EXAMPLE ABOVE
* Shows provided content only if the location
* criteria are met.
* ajax_geotwp_filter(array(country => 'US,CA'),'content');
* ajax_geotwp_filter(array(region => 'my_region'),'content');
*
* @param $args
* @param $content
*
* @return string
function ajax_geotwp_filter( $args = [], $content = '' ) {
extract( wp_parse_args( $args, [
'country' => '',
'region' => '',
'exclude_country' => '',
'exclude_region' => '',
'html_tag' => 'div',
] ) );
return '<' . $html_tag . ' class="geot-ajax geot-filter" data-action="country_filter" data-filter="' . $country . '" data-region="' . $region . '" data-ex_filter="' . $exclude_country . '" data-ex_region="' . $exclude_region . '">' . do_shortcode( $content ) . '</' . $html_tag . '>';
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment