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'm using PHP Widget which allows PHP code to execute from a widget. I tried using this code to execute do_shortcode but it's not rendering

<?php 
$lat = get_field('woo_maps_lat');
$long = get_field('woo_maps_long');
echo do_shortcode('[forecast location="' . $lat .','.$long. '" measurement=\'C\']');        
?>

If I echo a variable with simply `echo $lat;' it works. So the PHP Widget is working fine. The problem is echoing the shortcode in the widget. I can add shortcoded just fine to a widget but I need to be able to add the php variables where they are.

I tried adding add_filter('widget_text', 'do_shortcode'); to my functions.php but it's still not rendering properly. Any suggestions?

Thanks

share|improve this question
    
why are you using single quotation marks for your measurement attribute? Try dropping the escaping slashes and just using the double quotation marks. –  Evan Mattson Jul 13 '12 at 19:58
    
That's what wunderground plugin uses in their example shortcode. Even if I remove that attribute it still doesn't work. –  Anagio Jul 13 '12 at 20:50
    
have you tried using the shortcode in your code with hardcoded values as a test? Why not update your question with the code for the shortcode callback function so we know what is going on there. –  Evan Mattson Jul 13 '12 at 21:05
    
Yes it works with hard coded values, it also works using do_shortcode placed into a template file. –  Anagio Jul 14 '12 at 6:30
    
Is this the plugin you're using? (btw, your Question should contain this info) . . . . . . . . . Also, get_field is a function from Advanced Custom Fields or is a function of yours? –  brasofilo Jul 14 '12 at 16:23
show 3 more comments

1 Answer

up vote 0 down vote accepted

The problem was the double quotes which are needed in normal shortcode usage in posts and pages for some reason when using do_shortcode the double quotes in the attributes where breaking the function. Working code below

<div>
<?php 
$lat = get_field('woo_maps_lat');
$long = get_field('woo_maps_long');
echo do_shortcode('[forecast location=' . $lat .','.$long. ']');        
?>
</div>
share|improve this answer
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.