Any Content Shortcode

Glenn Blog

A snippet to add any content (including HTML, PHP) as a WordPress shortcode for output onto your WordPress site. Simply open up your theme (child theme) functions.php file and add the following function:-

// Add any content shortcode

function any_content_shortcode(){ 
    ob_start();?>
ENTER YOUR CONTENT HERE
<?php
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_shortcode('anycontent', 'any_content_shortcode');

Then in your WordPress site’s content editor add the shortcode [anycontent] and your custom content will be outputted using ‘output buffering’.

Example Use: Output X Theme Tags anywhere on a post

// Add X Theme Tags

function any_content_shortcode(){ 
    ob_start();?>
 <footer class="entry-footer cf">
	<?php echo get_the_tag_list( '<p><i class="x-icon-tags"></i> Tags:</p>' ); ?>
	  </footer>
<?php
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_shortcode('xtags', 'any_content_shortcode');

In this case, the shortcode is [xtags]

WordPress Custom Shortcode Plugins

Of course, WordPress plugins can also come to the rescue here and good ones are Shortcake and Shortcoder, but it’s always nice to do this yourself and also save additional PLugin bloat on your WordPress site.

About the Author
Any Content Shortcode 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced Web Designer / Web Developer (Webflow, WordPress, Weebly & Custom), Front-end / Back-end Developer & New Media Specialist, with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries, built up over a number of years.