1
<script language=”javascript”>
if (country_code==’US’)
{
document.write("[adrotate banner="1"]");
}
else
{
document.write("[adrotate banner="2"]");
}
</script>

[adrotate banner="x"] are wordpress shortcodes used for the Adrotate plugin

This script doesen't work because somehow outputting [adrotate banner="x"] is not possible? It uses the globally saved variable country_code.

I've tried with both " and ' (and the inner using one of them and the outer the other), but with no luck.

Any way to work around this and still use JS?

2 Answers 2

3

Shortcodes only work server side. If you want to work around it, you can submit the value using AJAX, call do_shortcode() on it in your PHP, return the results as JSON, and then write to the page.

http://codex.wordpress.org/Function_Reference/do_shortcode

As a side note, your example has a syntax error because you have double quotes inside your string, since it won't process as a shortcode. You either need to escape it or use single quotes.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response! So the only way around this is to use PHP? Ive got a script that doing the same thing i was trying in JS, but I dont know how to globally save the variable $country_code, so that I can use it several times in a post or page, without having to load the geoip.inc and GeoIP.dat all the time. How would I set this up in the header and save the variable for later use? Will using PHP in pages in Wordpress slow things down? Should I use Exec-PHP for this? Thanks so much! Help is much appreciated!
code<?php require_once(“geoip.inc”); $gi = geoip_open(“GeoIP.dat”,GEOIP_STANDARD); $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); if($country_code == ‘US’ || $country_code == false) { echo adrotate_group(1); } else { echo adrotate_group(2); } ?>code
Use the code in your comment to implement a JSON request, the geoip stuff is all done on the server. You can use this as a guide justin.ag/technology/creating-ajax-functions-in-wordpress, but note that it has to originate from the same domain. If you want to do it from a different domain, you will have to use JSONP.
Appreciated for suggesting to use the built in method!
1

Try to use ` backtick (Template literals) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals?retiredLocale=it

<script language=”javascript”>
var option1 = `[adrotate banner="1"]`;
var option2 = `[adrotate banner="2"]`;

if (country_code=='US') {
    document.write(option1);
} else {
    document.write(option2);
}
</script>

also work with php code (workaround for single and double quote) :

<script language=”javascript”>
var option1 = `<?php echo do_shortcode('[adrotate banner="1"]'); ?>`;
var option2 = `<?php echo do_shortcode('[adrotate banner="2"]'); ?>`;

if (country_code=='US') {
    document.write(option1);
} else {
    document.write(option2);
}
</script>

1 Comment

Thanks to Andrea Ciocca (stackoverflow.com/story/andreaciocca)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.