WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

$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?


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

$lat = get_field('woo_maps_lat');
$long = get_field('woo_maps_long');
echo do_shortcode('[forecast location=' . $lat .','.$long. ']');        
share|improve this answer

Your Answer


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.