Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
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! –  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
add comment

1 Answer

up vote 2 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"]',
    $address,
    $city,
    $province,
    $postalcode,
    '100%'
);
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
show 4 more comments

Your Answer

 
discard

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.