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)