WordPress quick tip #3 – using plugin short codes within template files

Did you know that it’s possible to place a plugin shortcode inside a WordPress theme file? It’s very easy to do, and a great time saver in situations where you’ve found a plugin with exactly the functionality you were searching for only it delivers that functionality via a shortcode. What if you want that functionality in a php theme file rather that within the content area of a post or page? There is a way.

For those who just want the code, here it is:

<?php echo do_shortcode('[shortcode option1="value1" option2="value2"]'); ?>

A big thanks to wp-cms.com and Michael Dozark for writing the posts I found when searching for the solution.

In this short and sweet piece of code, do_shortcode() tells WordPress to process the shortcode code which would usually be placed within the content of a post or page. The shortcode simply needs to be placed inside the () brackets and wrapped in single quote marks. Replace the shortcode, between the [ ] brackets in the example code above, with the shortcode of the plugin you are using, including any options, then place the whole snippet of code into the theme file.

There is documentation on the shortcode API and the do_shortcode() function in the WordPress codex here

Tags: