0

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

8
  • why are you using single quotation marks for your measurement attribute? Try dropping the escaping slashes and just using the double quotation marks. Commented Jul 13, 2012 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
    Commented Jul 13, 2012 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. Commented Jul 13, 2012 at 21:05
  • Yes it works with hard coded values, it also works using do_shortcode placed into a template file.
    – Anagio
    Commented Jul 14, 2012 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
    Commented Jul 14, 2012 at 16:23

1 Answer 1

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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