Using do_shortcode with variables?

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.

Related posts

Leave a Reply

1 comment

  1. 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. 🙂