Using AJax var to insert shortcodes by country
-
Hey Benjamin.
Thanks for the great plugin. Hope You are doing alright.
I’ve been struggling for a couple days trying to implement some ajax for a simple task. I’m not so familiar with JS so it’s been a TASK.
The goal: insert a shortcode to display an affiliate product offer into posts content, diferent code for DE, US and FR. Simple.
The problem: Site is using cache (WP Fastest Cache plugin). So using shortcodes won’t work. Disabling cache on sites with geolocation will end disabling the cache on the whole site since this offers are site-wide.
The approach: I’ve been trying to implement modified ajax from the documentation examples, ie:
jQuery(document).ready(function($) { geoip_detect.get_info().then(function(record) { if (record.error()) { console.error('WARNING Geodata Error:' + record.error() ); $('.geo-error').text(record.error()); } // some vars var country = record.get('country.iso_code'); var thecode = 'default-shortcode'; // asign the shortcode if (country == 'DE') { thecode = 'shortcode-for-DE'; } else if (country == 'FR') { thecode = 'shortcode-for-FR'; } else if (country == 'US'){ thecode = 'shortcode-for-US'; } // kept for test purpose $('.geo-ip').text(record.get('traits.ip_address')); }); });
this code works as far as detecting the country and setting the variable value. I don’t know if this is the right or cleanest way to accomplish the goal, but is all I got by this time.
The issue: Is it possible to retrieve “thecode” variable value from the jsQuery into a php snippet? So, I could just call an execute an specific shortcode via:
echo do_shortcode( sprintf( '["%"]', $thecode ) );
Thanks. Hope You can shed some light here
- The topic ‘Using AJax var to insert shortcodes by country’ is closed to new replies.