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

Struggling to get the format right for the PHP in this do_shortcode embed using variables from Advanced Custom Fields plugin. I think I've tried every variation on ., ', and ". After reading every resource I could find I'm still no closer.

    $address = get_field('cong_streetaddress');
    $city = get_field('cong_city');
    $province = get_field('cong_province');
    $postalcode = get_field('cong_postalcode');

    echo do_shortcode('[pw_map address="'" . $address . " . " . $city . " . " . $province . " . " . $postalcode . "'" width="100%" height="200px"]'); 

Doing it with just one variable, like so echo do_shortcode('[pw_map address="'. $postalcode . '" width="100%" height="200px"]'); works fine.

share|improve this question
Please add the callback function for this shortcode to your question. It will be much easier then to give a useful answer. Oh, and welcome to WordPress Stack Exchange! – toscho Oct 7 '12 at 19:59
Thanks very much for the welcome and for your help. This is quite the resource - I'm amazed. The shortcode is generated by a plugin called simple google maps short code wordpress.org/extend/plugins/simple-google-maps-short-code. – Jim Kite Oct 7 '12 at 20:04
up vote 3 down vote accepted

Your syntax is broken, the quote marks do not match. Try to separate data from the shortcode template, and use sprintf():

$shortcode = sprintf(
    '[pw_map address="%1$s %2$s %3$s %4$s" width="%5$s" height="200px"]',
echo do_shortcode( $shortcode );

That’s much easier to read, and it is harder to create syntax errors. :)

share|improve this answer
It doesn't seem to return anything (I think because it doesn't have values for any of those variables). I edited like so: <?php $address = get_field('cong_streetaddress'); $city = get_field('cong_city'); $province = get_field('cong_province'); $postalcode = get_field('cong_postalcode'); $shortcode = sprintf( '[pw_map address="%1$s %2$s %3$s %4$s" width="100%" height="200px"]', $address, $city, $province, $postalcode ); echo do_shortcode( $shortcode ); ?> but that didn't work either. – Jim Kite Oct 7 '12 at 19:51
Oops. How does one format code blocks in comments? – Jim Kite Oct 7 '12 at 19:52
One does not simply code into Comments – Tom J Nowell Oct 7 '12 at 19:54
@JimKite The % in 100% might have mixed up sprintf(). Try the changed code now. – toscho Oct 7 '12 at 20:05
The percentage mark change doesn't seem to have made any difference as far as I can see. It still doesn't return anything to the page. Was I correct to be declaring the variables before this code? – Jim Kite Oct 7 '12 at 20:50

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.