Redesigning Poles Apart Design, I used the Datafeedr Random Ads V2 plugin as a random image rotator. The plugin requires that I add PHP code to a template page which I didn’t want to do because I wanted the image rotators to appear in specific posts. I could make that happen by installing and enabling another plugin that enables me to use PHP code within a post, but sometimes using PHP code within a post can cause unexpected issues, plugin or no. So, I thought, oh yeah, shortcodes. Shortcodes are pieces of simplified code that represent PHP code/functions without having to use actual PHP in the place you put the shortcode. Actually, you can use shortcodes for output of regular text, html and even CSS. A shortcode comes in this format [shortcode-post-title-here]. Here’s how to add a PHP function into a post from any plugin that requires a specific function call (which is almost all) to generate whatever content the plugin is for.
Usually a WordPress plugin requires you to add a template code such as :
if (function_exists('function-name-here')) { echo function-name-here('function_parameters_here'); }
or simply
function-name-here('function-parameters-here'); }
somewhere in one of your PHP template files. But, sometimes you want to add the function in a post given whatever you’re using the plugin for. If the result of the plugin you’re using needs to appear within a post content or page content, do the following:
- Add
require_once(TEMPLATEPATH . '/includes/shortcodes.php');to your functions.php file if you haven’t already - Create a directory /includes/shortcodes.php inside your theme template (inside your themes folder, create a folder called “includes” and within that includes folder ad a file called “shortcodes.php”
- Then add the following code inside the your shortcodes.php file:
function shortcode-function-title($atts) {
return function-name-here('function-parameters-here');
}
add_shortcode('shortcodpost-title-here', 'shortcode-function-title-here');
Remember, remove any return/breaks before or after the beginning and ending php tags otherwise you’ll get an error. - Replace Replace
shortcode-function-titlewith whatever title you want to name this function; Replaceshortcodpost-title-herewith whatever title you want to name the shortcode text that will go inside the actual post; Replacefunction-name-here('function-parameters-here');with the code required to go into the template file as instructed by the creator of the plugin you’re using. - Finally, add the following
[shortcode-post-title-here]to your post where you want the result of the function to appear. That’s it!

3 responses so far ↓
1 webtha // Oct 9, 2009 at 11:58 am
Good information.
2 webdesignsydney // Jan 18, 2010 at 9:47 pm
thank you for explanation how to use shortcode, it is really useful, especially you provide all codes for us so should be easy.
3 Miklas Njor // Feb 10, 2010 at 1:34 pm
Thank you for the post. It’s so nice to get these more advanced topics covered.
It would be nice though, if you could include a peice of real life function code just to see what it would look like.
Leave a Comment