1

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.

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

2
  • 1
    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!
    – fuxia
    Commented Oct 7, 2012 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
    Commented Oct 7, 2012 at 20:04

1 Answer 1

5

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"]',
    $address,
    $city,
    $province,
    $postalcode,
    '100%'
);
echo do_shortcode( $shortcode );

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

9
  • 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
    Commented Oct 7, 2012 at 19:51
  • Oops. How does one format code blocks in comments?
    – Jim Kite
    Commented Oct 7, 2012 at 19:52
  • One does not simply code into Comments
    – Tom J Nowell
    Commented Oct 7, 2012 at 19:54
  • @JimKite The % in 100% might have mixed up sprintf(). Try the changed code now.
    – fuxia
    Commented Oct 7, 2012 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
    Commented Oct 7, 2012 at 20:50

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.