Adding Shortcodes to a WordPress Template

written by Michael Lautman on May 29, 2012 in Web Design with no comments">no comments
Pinterest

In WordPress, a shortcode is a bit of text, usually in square brackets, that allows you to include some pre-defined function on a page or in a post.  What’s neat is that you don’t need to include all of the code (javascript, php or whatever), you can just use the shortcode.  Wordpress has a few shortcodes built in, namely the <re>[galler]</pre> and<pre> [video]</pre> shortcodes.  There are loads of plugins that let you include shortcodes for styles (font, size, colors), elements like buttons or forms and columns.  The list is extensive.   The WordPress shortcode API even lets you define your own shortcodes.

Shortcodes are especially useful for elements that you want to include on a specific page or post, but there may be times where you want to include them in the header or footer of your site, or even on a specific page or post template.

In order to do this you simply inlcude the following bit of code where you want the shortcode to appear:

<?php echo do_shortcode('[shortcode]'); ?> 

You might find it useful to wrap the shortcode in a div for styling purposes, like this:


<div id="shortcode"><?php echo do_shortcode('[shortcode]'); ?></div>

Now you can just reference the div in your css file.

Pinterest