Using Output Buffer in Shortcode

If you’re using a shortcode to display a customize content with lots of HTML tags output buffering is your tool. It’s a common Issue when using a shortcode is we echo it’sĀ output, in return the output doesn’t follow the layout of the builder.

function display_me_properly_callback(){
  ob_start();
  ?>
  <h2>Hello</h2>
  <p>This is a test.</p>
  <?php
  return ob_get_clean();
}
  
add_shortcode("display_me_properly","display_me_properly_callback");

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.