PHP using a variable inside do_shortcode() - Wordpress Page Template

Lucas Mercer picture Lucas Mercer · May 19, 2015 · Viewed 15.7k times · Source

The format for the shortcode that I wish to do is: [product id="id#"] The format for do_shortcode is: do_shortcode('[shortcode');

My question is how can I insert a PHP variable as the id#?

I have a variable $itemid that I would like to use like this:

echo do_shortcode('[product id="$itemid"])

But I cannot for the life of me get it to work. Any suggestions would be greatly appreciated.

Answer

Uri Goren picture Uri Goren · May 19, 2015

This is an issue of single quotes vs double quotes,

see What is the difference between single-quoted and double-quoted strings in PHP?

this fix should work:

echo do_shortcode("[product id=\"$itemid\"]")