Shortcodes are very effective way to include dynamic content into your posting. This tutorial will guide you through the process of creating your own shortcode. Just in few easy steps.
In just few minutes you will create a shortcode that includes the post publication date. When you include [post-date] in your post, WordPress will replace it automatically with the post date.
1. Create a new post
Let’s start with creating a post to play with. Publish the post. Here is the post I created.

For this tutorial I am using the default WordPress theme. At the end of the post I have inserted [post-date] shortcode.
Because the shortcode is not implemented yet, WordPress displays it literally without replacing it.
2. Create a new plugin.
Plugins are placed under WordPress plugins directory. It is good practice to place each plugin into a separate directory.
Create a new directory named post-date under WordPress plugins directory (wp-content/plugins).
Using a text editor, create a text file named post-date.php. You can use any file name that you like, but for this tutorial we will use the post-date.php.
Now you have an empty text file: wp-content/plugins/post-date/post-date.php. This file will contain the code for the shortcode handler.
3. Define the plugin
To identify plugins WordPress uses comments that conform to special format. Enter the following plugin definition in the post-date.php file.
<?php /* Plugin Name: Post-date short code Plugin URI: http://devcorner.georgievi.net Description: This plugin defined a 'post-date' shortcode handler. Author: Ivan Georgiev Version: 1.5.1 Author URI: http://devcorner.georgievi.net/ */ ?>
Now you have a plugin defined. You are able to see it under Plugins list in WordPress administrator.
4. Add shortcode handler code
We will create a PHP function that will be invoked when WordPress needs to replace a post-date shortcode.
<?php /* Plugin Name: Post-date short code Plugin URI: http://devcorner.georgievi.net Description: This plugin defined a 'post-date' shortcode handler. Author: Ivan Georgiev Version: 1.5.1 Author URI: http://devcorner.georgievi.net/ */ function post_date_handler($atts) { global $post; return mysql2date('M jS, Y', $post->post_date); } add_shortcode('post-date', 'post_date_handler'); ?>
The add_shortcode function instructs WordPress to invoke the post_date_handler function when it needs to replace a post-date shortcode.
The post_date_handler function returns the formatted date for the current post.
Now you have everyting ready. If you try to view the post you created at the beginning of this tutorial, you will see no change. Why?
One small thing left. You need to activate the newly created plugin.
5. Activate the plugin
Go to the Plugins section in WordPress administrative section. In the plugin list find the ‘Post-date shortcode’ plugin and click the ‘Activate’ link to activate it.
Open the post and voila! You see that the shortcode text is replaced by the post date.

If you want to include ‘[post-date]‘ as a text, you need to use double brackets. You enter ‘[[post-date]]’ to get ‘[post-date]‘.
This tutorial covers only WordPress shortcode basics. For more detailed description see WordPress shortcode API.

Add A Comment
You must be logged in to post a comment.