Doing ‘Includes’ in WordPress

If you want to repeat a section of HTML code in your WordPress posts or pages you can either copy the code onto every page (yawn) or use a shortcut code (horray). A shortcut code acts in the same way as an ‘include’ in regular coding and pulls in a segment of HTML or text from an external file. In this case the external file is ‘functions.php’ in WordPress.

I recently used it to solve a mysterious ‘disappearing code’ problem in WordPress. A link for an image rollover would vanish from the code editor in a random and very annoying way. Replacing this rollover code with a shortcut code was a neat way of solving this problem.

Here’s how to do it.

In functions.php add the following.

add_shortcode(‘quote’, ‘short_quote ‘);
# This line adds a shortcut code called ‘quote’ and a function called ‘short_quote’ – you can change these names to reflect your own code.

function short_quote () {
return ‘ put your text and code here ‘;

Then in our WordPress posts and pages you can add the shortcode [quote] and this will display the code you added where it says ‘put your text and code here ‘.

And that’s it! I usually stick my custom functions at the end of the functions.php file so they are easy to find.

This entry was posted in General Information. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Both comments and trackbacks are currently closed.