WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to pass a variable value to a shortcode attribute. I can't get it to work.

Is the following supposed to work, or must values be literal?

$mycat = 'books';
$myshortcode = '[products category="'.$mycat .'"]';

echo do_shortcode($myshortcode);

The above outputs nothing. If I replace it with

$myshortcode = '[products category="books"]';

echo do_shortcode($myshortcode);

Then it produces the expected result.

I'm placing this code in a 'Custom Code Block' in the HeadWay Theme's Visual editor. I'm trying to find out if the problem I have has to do with the way HeadWay parses the content of the custom code block, or if it is indeed not possible to construct a string for use with do_shortcode() like this.

share|improve this question
Please add debug information. – toscho Sep 9 '12 at 1:41

I would simply find the function that the shortcode is pointing to and call it directly with your variable. It seems like there would be less room for error.


share|improve this answer
I though about that approach, but I wanted to keep compatibility with the shortcode in the unlikely event that the function should be renamed in a later version of the plugin that defines the shortcode. I can see though, that this is probably as unlikely as the actual shortcode being renamed. – mikkelbreum Sep 8 '12 at 21:51
Glad you got it running...good point about the possibility of the function name changing. – Douglas.Sesar Sep 8 '12 at 22:03
up vote 0 down vote accepted

After more probing I found out, that the problem must be with the way HeadWay is parsing the content of the custom code block.

I still don't understand it exactly, but changing the code from this:

$mycat = 'books';
$myshortcode = '[products category="'.$mycat .'"]';

echo do_shortcode($myshortcode);

to this

$mycat = 'books';
$myshortcode = 'products category="'.$mycat .'"';

$myshortcode = '[' . $myshortcode . ']';

echo do_shortcode($myshortcode);

made it work...

share|improve this answer
That.. Isn't possible. Something must be wrong with your code elsewhere. – Otto Sep 9 '12 at 2:05
Well it's a fact that changing the code from the first example to the second made it work. But remember, I'm not placing this code inside a regular php file, but inside a 'Costom Code' block using the Visual Editor of the HeadWay theme, and it must be because Headway 'picks up' the shortcode in the first example, and processes it, turning the value of $myshortcode into the result of the processed shortcode, ...or something.. That's the only explanation I can think of. – mikkelbreum Sep 9 '12 at 19:27
Well, that makes more sense, although I can't understand why the theme would allow PHP code in a text field and also process shortcodes in that same text field. – Otto Sep 10 '12 at 10:42
no.. but that is exactly what it does, even if the shortcode is inside <?php ?> tags. – mikkelbreum Sep 10 '12 at 12:30
@mwb Since your question has been solved, could you please accept your own answer to let other people that are having the same/a similar problem know it's been solved (and also to prevent this question from being picked up again by the community bot). – tfrommen Apr 7 '13 at 13:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.