How to use WordPress shortcodes in page/theme templates

Plugins and widgets enable users to take advantage of a huge array of extra or additional functionality – from displaying a simple list of popular posts to conducting user polls or inserting complex forms. But what if you’d like to use some of that extra functionality somewhere other than in a page, post or sidebar widget? Like, say, in a WordPress page/theme template file?

Well, in much the same way that shortcodes (something we’ve already covered in relative depth in an earlier post) can often be used to take advantage of a plugin or widget’s functionality from within the WordPress visual editor (assuming the author of the plugin or widget in question created a shortcode for use with their plugin/widget in the first place), they can ALSO be used to insert the same functionality into a WordPress page/theme template…

Here’s how:

To use a shortcode in a page/theme template, simply wrap the standard WordPress do_shortcode function (created for this very purpose) with a little PHP coding – as follows (to be inserted wherever in the page/theme template file you’d like to enable your shortcode’s specific functionality):

<?php echo do_shortcode("[insert-your-shortcode-here]"); ?>

…remembering of course, to substitute [insert-your-shortcode-here] for the shortcode you actually want to use.


Tweet about this on TwitterShare on FacebookGoogle+Share on LinkedInEmail to someone

By Brin Wilson

Founder of WinningWP - passionate about all things WordPress! Active on both Twitter and Google+ (preferred).
Comments (policy)
More in Code, Quick Tips
CSS For Beginners: Where To Learn CSS Online

A few years back while I was walking through a local bookstore, I came across a book that grabbed my attention. Curiosity then nudged me...