1

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 ?

3
  • 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. Commented Apr 13, 2017 at 12:33
  • Hi @JoseCarlosRamosCarmenates, please check the update Commented Apr 13, 2017 at 12:36
  • 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? Commented Apr 13, 2017 at 15:48

1 Answer 1

0

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @JoseCarlosRamosCarmenates, but the output is exactly the same :(
@DDE What's the output expected?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.