How to use WordPress shortcodes in page/theme templates
- By
- 8 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…
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.
Simple!
Leave a Reply
All comments are held for moderation. We'll only publish comments that are on topic and adhere to our Commenting Policy.
Life saver! ‘ instead of ” worked for me!!!
Most likely your template spans across multiple php files and you customize. This function is not required. Merely, just type the shortcode in the html. The function will actually NOT work in customized plugins
Thanks for this but is it ok for WP 4.8 ? I get the shortcode but nothing displays. Thanks !
Thanks bro I was looking for it to develop my next wordpresss plugin
Hmmm… should be using ‘ not ” to wrap the shortcode.
Either will work… What are you referencing for this ‘correction’? Most other sites seem to agree with me here, such as: https://css-tricks.com/snippets/wordpress/shortcode-in-a-template/ etc
Check out the replies in the css-tricks link, seems like if the shortcode has options, then you must use single quote instead of double quote.
– interesting. Thanks.