Output a shortcode inside an echo for wordpress

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.

Read More
<?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

Related posts

1 comment

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

    function single_location_info_shortcode( $atts ){
        // First, start the output buffer
        ob_start();
    
        // Then, run the shortcode
        do_shortcode( '[map id="1"]' );
        // Next, get the contents of the shortcode into a variable
        $map = ob_get_clean();
    
        // Lastly, put the contents of the map shortcode into this 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>
                        ' . $map . '
                    </div>
                </div>';
         }
    

    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:

    function single_location_info_shortcode( $atts ) {
        // By passing through the 'the_content' filter, the shortcode is actually parsed by WordPress
        return apply_filters( 'the_content' , '<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>
                        [map id="1"]
                    </div>
                </div>' );
         }
    

Comments are closed.