1
votes

I would like to use the following shortcodes:

[section]
<h1>Section body content</h1>
[section_footer]<p>Section footer content</p>[/section_footer]
[/section]

So, I have added the following shortcode callbacks:

function sec_cb ($att, $content = null) {
// ...
return 
'
<section>
<div class="sec-body">
'.strip_shortcodes($content).'
</div>
'.do_shortcode($content).'
</section>
';
}

function sec_footer_cb ($atts, $content = null) {
return 
'
<div class="sec-footer">
'.$content.'
</div>
';
}
add_shortcode('section', 'sec_cb');
add_shortcode('section_footer', 'sec_footer_cb');

But the output is:

<section>
 <div class="sec-body">
  <h1>Section body content</h1>
 </div>
 <h1>Section body content</h1>
 <div class="sec-footer">
  <p>Section footer content</p>
 </div>
</section>

How can I fix it and print in footer only footer shortcode content ?

1
Maybe you have registered both function on the same hook do_shortcode. Please Update the Post and set how/where you call the functions sec_cb and sec_footer_cb. - Jose Carlos Ramos Carmenates
Hi @JoseCarlosRamosCarmenates, please check the update - Danny
Right now you are print in the footer the correct content see: <div <p>Section footer content</p></div> you don't have de section content like <h1>Section body content</h1> Please what's the output needed? - Jose Carlos Ramos Carmenates

1 Answers

0
votes

Please try add ob_start() and ob_get_contents() and ob_end_clean(), I adjust your code with that. Try and let me know if it work.

function sec_cb ($att, $content = null) {
    // ...
    ob_start();
    echo '<section>
            <div class="sec-body">
               '.strip_shortcodes($content).'
            </div>
            '.do_shortcode($content).'
          </section>';
    $eval_result = ob_get_contents();
    ob_end_clean();
    return $eval_result;
}

function sec_footer_cb ($atts, $content = null) { 
    ob_start();
    echo '<div class="sec-footer">
            '.$content.'
        </div>';
    $eval_result = ob_get_contents();
    ob_end_clean();
    return $eval_result;
}

add_shortcode('section', 'sec_cb');
add_shortcode('section_footer', 'sec_footer_cb');