I am trying to write a shortcode that has another shortcode nested inside of it. The [map id=”1″] shortcode is being generated from a different plugin but I want to have the map display when I execute this shortcode.
I don’t think this is the best way to go about this but I am still new to php coding.
<?php
add_shortcode( 'single-location-info', 'single_location_info_shortcode' );
function single_location_info_shortcode(){
return '<div class="single-location-info">
<div class="one-half first">
<h3>Header</h3>
<p>Copy..............</p>
</div>
<div class="one-half">
<h3>Header 2</h3>
<p>Copy 2............</p>
<?php do_shortcode( '[map id="1"]' ); ?>
</div>
</div>';
}
?>
I dont think I should be trying to call php from within a return…. I though I read somewhere that I should use a “heredoc” but I have been unable to get it to work correctly.
Any thoughs?
Thanks
Your hunch is right. Don’t return a string with a php function in the middle of it. (Not very readable, and the sample code above won’t work)
A heredoc won’t solve this issue. While useful, heredocs really are just another way of building a string in PHP.
There are a few potential solutions.
The “PHP” solution is to use the output buffer:
ob_start
ob_get_clean
Here’s your modified code that will do what you are asking:
Alternate Method
The “WordPress way” of doing this would be to embed the shortcode in the content string, and run it through the WordPress the_content filter function: