metinsaylan // rogue developer

HowTo: Use shortcodes in your theme files

Altough shortcodes are made to be used in post content, you can sometimes need it’s functionality in your WordPress theme designs. Here I will show you how to easily use any shortcode in your page templates.

To use a shortcode in our theme templates, we use WordPress shortcode evaluator function do_shortcode. This function takes any text string and it evaluates shortcodes inside the text. Here is how to put a leaderboard ad unit in your page template using shortcodes:

<?php echo do_shortcode('[adsense type="leaderboard"]'); ?>

do_shortcode will convert that shortcode to output, and we echo the output.

NOTE : If you forget echo it won’t display anything. This is a common mistake.

You can put the following wrapper function in your functions.php, to make it easier to use later on:

function shortie( $shortcode ){ echo do_shortcode( $shortcode ); }

Then in your templates you can use:

<?php shortie('[adsense type="banner" align="center"]') ?>

I hope you found this trick useful. Enjoy!

Search HowTo: Use shortcodes in your theme files on Google