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.


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.


  • 1
    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. Sep 8 '12 at 21:51
  • Glad you got it running...good point about the possibility of the function name changing. Sep 8 '12 at 22:03

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

  • 2
    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. 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. 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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