Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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);
share|improve this answer
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. –  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
add comment
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
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. –  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). –  t f Apr 7 '13 at 13:43
add comment

Your Answer

 
discard

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.