Wordpress: Outputting shortcode with Javascript?

2019-09-05 09:05发布

<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?

1条回答
在下西门庆
2楼-- · 2019-09-05 09:34

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.

查看更多
登录 后发表回答