How to use WordPress shortcodes in page/theme templates
- Last updated:
- 4 Comments
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…
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.