Have you ever needed to add some custom javascript or php code to a post or page in WordPress. If you try and add it to the Visual or Text editor, it won’t work. Fear not, there is an easy solution.

Sitepoint.com recommends creating a page called shortcodes.php that contains your custom code and save this within your themes folder. Then carefully edit your functions.php file located in the same directory and add a function to the bottom that calls the shortcodes.php file. Below is a simple ‘Hello World’ example:

Add:

include('shortcodes.php');

to the functions.php file.

Then add the following code to your shortcodes.php file:

<?PHP 
/* Simple Hello World Example */
function HelloWorldShortcode() {
	return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode'); 
?>

Now whenever you want to add that custom code to a post or page in WordPress, just type in the shortcode [helloworld] and it will call the custom code from your shortcodes.php file. Pretty slick!