Add [map id="1"] shortcode inside a custom shortcode

I wrote a custom shortcode that I will be using on multiple pages throughout our site. Basically I am trying to set up so if I want to change the following code I only have to change it in one location.

Here is my code:

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>About this chart</h3>
		<div class="about-chart-copy">Exact timing of applications would be determined by total chill portions and other factors. Consult your Pest Management Adviser, PCA, or U.C. Farm Adviser for more information.</div>
		<a class="button">Print This Page</a>
		<a href="/disclaimer/">Disclaimer</a>
		</div>
	<div class="one-half">
		<h3>Logger Map</h3>
		<a href="/locations/">See all locations</a>
		[map id="1"]
	</div>
</div>';
	}

Its outputting everything but [map id="1"] correctly. I was reading online and came across this (here),

function caption_shortcode( $atts, $content = null ) {
   return '<span class="caption">' . do_shortcode($content) . '</span>';
}

but I'm not sure how to implement it with the maps shortcode. Any suggestions?

Thanks,
William

  • Tyler Postle

    Hey William,

    What was the syntax error? That should pin point what the issue is. I spoke to a developer and nesting shortcodes like this shouldn't be a problem. It might be the PHP opening tag that is causing the problem, if there was already an opening tag further up.

    Would you be able to paste the full code from the file in here? A long with the error. Then we should be able to get this all sorted out.

    Talk to you soon!

    Cheers,
    Tyler

  • William

    Hi @Tyler Postle,

    I tried super hard to solve this one my self. I came up with 4 options but none of them worked... :slight_frown: Maybe you or the developer could point me in the correct direction or fix any of my mistakes...

    <?php
    //Option 1 - Santax error on the line of php do_shortcode( '[map id="1"]' );  - No other explenation
    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>About this chart</h3>
    		<div class="about-chart-copy">Exact timing of applications would be determined by total chill portions and other factors. Consult your Pest Management Adviser, PCA, or U.C. Farm Adviser for more information.</div>
    		<a href="/disclaimer/">Disclaimer</a><br>
    		<a class="button" href="javascript:window.print()">Print This Page</a>
    		</div>
    	<div class="one-half">
    		<h3>Logger Map</h3>
    		<p><a href="/locations/">See all locations</a></p>
    		<?php do_shortcode( '[map id="1"]' ); ?>
    		</div></div>';
    	}
    ?>
    <?php
    //Option 2 - Page loded suprt wrong. The shortcode loaded the columns above graph shortcode (graph shortcode being pulled by another plugin but being loaded to the same page.
    function caption_shortcode( $atts, $content = null ) {
       return '<span class="caption">' . do_shortcode($content) . '</span>';
    }
    
    add_shortcode( 'single-location-info', 'single_location_info_shortcode' );
    	function single_location_info_shortcode(){
    		echo '<div class="single-location-info">
    	<div class="one-half first">
    		<h3>About this chart</h3>
    		<div class="about-chart-copy">Exact timing of applications would be determined by total chill portions and other factors. Consult your Pest Management Adviser, PCA, or U.C. Farm Adviser for more information.</div>
    		<a href="/disclaimer/">Disclaimer</a><br>
    		<a class="button" href="javascript:window.print()">Print This Page</a>
    		</div>
    	<div class="one-half">
    		<h3>Logger Map</h3>
    		<p><a href="/locations/">See all locations</a></p>';
    		echo do_shortcode( '[map id="1"]' );
    		echo '</div></div>';
    	}
    ?>
    <?php
    //Option 3 - did not load corretly on the page. Columns loaded in header.
    function single_location_part_one (){
    	return '<div class="single-location-info">
    	<div class="one-half first">
    		<h3>About this chart</h3>
    		<div class="about-chart-copy">Exact timing of applications would be determined by total chill portions and other factors. Consult your Pest Management Adviser, PCA, or U.C. Farm Adviser for more information.</div>
    		<a href="/disclaimer/">Disclaimer</a><br>
    		<a class="button" href="javascript:window.print()">Print This Page</a>
    		</div>
    	<div class="one-half">
    		<h3>Logger Map</h3>
    		<p><a href="/locations/">See all locations</a></p>';
    }
    $part_one = single_location_part_one();
    
    function single_location_part_two (){
    	return '</div></div>';
    }
    
    $part_two = single_location_part_two ();
    
    add_shortcode( 'single-location-info', 'single_location_info_shortcode' );
    	function single_location_info_shortcode(){
    		echo $part_one;
    		echo do_shortcode( '[map id="1"]' );
    		echo $part_two;
    	}
    ?>
    <?php
    //Option 4 I tried to use a "heredoc" and it returns """""" on the page
    function single_location_part_one (){
    	return '<div class="single-location-info">
    	<div class="one-half first">
    		<h3>About this chart</h3>
    		<div class="about-chart-copy">Exact timing of applications would be determined by total chill portions and other factors. Consult your Pest Management Adviser, PCA, or U.C. Farm Adviser for more information.</div>
    		<a href="/disclaimer/">Disclaimer</a><br>
    		<a class="button" href="javascript:window.print()">Print This Page</a>
    		</div>
    	<div class="one-half">
    		<h3>Logger Map</h3>
    		<p><a href="/locations/">See all locations</a></p>';
    }
    $part_one = single_location_part_one();
    
    function single_location_part_two (){
    	return '</div></div>';
    }
    $part_two = single_location_part_two();
    
    function single_location_map (){
    	echo do_shortcode( '[map id="1"]' );
    }
    $part_map = single_location_map();
    
    add_shortcode( 'single-location-info', 'single_location_info_shortcode' );
    	function single_location_info_shortcode(){
    		echo <<<EOL
    		"$part_one"
    		"$part_map"
    		"$part_two"
    EOL;
    	}
    ?>

    Thanks,
    William

  • William

    @Tyler Postle

    I figured it out with a little help from Stack Overflow and the WordPress codex :smiley:.

    Answer:
    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>' );
         }

    Hope this helps someone else in the future.

    Thanks,
    William