Create WordPress shortcode to display out HTML on pages and posts

Sometimes we want to display the same HTML content several times on the website, we can either copy & paste the HTML content and added to the WordPress text editor which most of the times works or we can just create a WordPress shortcode to display the HTML content.
Using WordPress shortcode we can avoid having the text editor full of HTML code which sometimes could be very painful to work out 🙂

Add this code below on your theme functions.php and use this shortcode [test_print_shortcode] on your posts, pages text widget.
Note: the function name needs to be changed to something more meaningful.

Create WordPress shortcode to display out HTML on pages, posts and text widgets code snippet:

/**
 *
 * Shortcode to display out HTML
 */
if ( ! function_exists( 'ns_shortcode_html' ) ) {
function ns_shortcode_html() {
 ob_start();

  ?>
  <div class="event__inner_header">
    <h3 style="text-align:center;">THANK YOU !</h3>
  </div>
 <?php
 return ob_get_clean();
}
add_shortcode( 'test_print_shortcode', 'ns_shortcode_html' );
}

You can find more info about the Shortcode API here.

Thank you for seeing my tutorial and feel free to share and comment :). Do you have a code snippet and you want to see it published on my site? I will be more than happy to do it please send me a message, (here)

ABOUT AUTHOR

Nuno

I am a Freelance Web Developer and WordPress Expert based in London with a wealth of website development and support experience. I am great at problem solving and developing quick solutions.

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.

  • LET'S TALK!

    Fill in the form below to make an enquiry or find my contact details on my contact page.