How can I be a good

WordPress Shortcode

Jon Volks
Written By | @webproninja

What is WordPress Shortcode? Shortcode is a great way for you as a programmer to give your every day general users, a way to put in dynamic content that might normally to hard for them. If you haven’t seen it before, in your WordPress WYSIWYG (editor), you may have seen something that looks like this [something here]. Many photo galleries use it and Gravity Forms is a big component of it as well. Lets take a look on how to create a simple shortcode.

In this example, lets see how we can enter in the current date.  Within the functions.php file of your theme, add this line of code (you may or may not need the opening/closing PHP lines depending on if there is any other PHP code within it.

function output_date() {
return date('m/d/Y');
add_shortcode('current_date', 'output_date');

So what is happening here. First we created a simple PHP function called output_date(). All this function did was return the current date in m/d/Y format. The last line, add_shortcode(), simply created the shortcode that we are going to use. The first parameter, current_date, is the shortcode and the second parameter, output_date, is the function that is referenced. It is now installed and ready to be used. In the WYSIWYG editor, just add [current_date] and when the page is called, it will output the current date.

This was just a very simple example of how shortcode works. There are many other things that can be done with it, such as passing in parameters into the shortcode for different results. The sky is the limit. For more information, check out the WordPress Codex on shortcode and also follow my blog as I will be posting more about shortcode in the future.

Do you have a favorite shortcode you like to include for your users? Tell us about it below.

About The Author

Jonathan Volks is currently a Web Programmer at Skidmore College in Upstate New York. He enjoys everything about the Internet; from Search Engine Optimization to Web Programming. His hobbies include landscape photography and hiking the Adirondacks.

Leave a Reply

Your email address will not be published. Required fields are marked *