0

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.

1
  • Please add debug information. Commented Sep 9, 2012 at 1:41

2 Answers 2

0

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.

example:

actual_products_shortcode_function($mycat);
2
  • 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. Commented Sep 8, 2012 at 21:51
  • Glad you got it running...good point about the possibility of the function name changing. Commented Sep 8, 2012 at 22:03
0

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

5
  • 2
    That.. Isn't possible. Something must be wrong with your code elsewhere. Commented Sep 9, 2012 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. Commented Sep 9, 2012 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. Commented Sep 10, 2012 at 10:42
  • no.. but that is exactly what it does, even if the shortcode is inside <?php ?> tags. Commented Sep 10, 2012 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). Commented Apr 7, 2013 at 13:43

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.