What is WordPress Shortcode and Why to Use Them
WordPress shortcodes are one of the most useful features provided by WordPress. Shortcodes were introduced in version 2.5 and since then it is there. Lots of people using WordPress may not have proper knowledge of shortcodes. So in this article I would walk through basic knowledge of shortcodes and not much in technical details.
What is Shortcode in WordPress
If you ask me a simple definition of shortcode I would say: “Shortcodes in WordPress are the macros, presented in square brackets ([ ]), which can be inserted in a post or page to be replaced with dynamic content by WordPress engine during run time.” When a shortcode is inserted in a wordpress post or page then we are instructing wordpress engine to replace the shortcode with a dynamic content produced by a PHP function. There shortcodes are written inside a square bracket ([ ]).
For example: Lets say you want to display some random posts in your article, then your shortcode may look something like this: [random-post].
The use is simple, whenever you want to display some random posts within your new post then you can simply add [random-post] anywhere inside your post content. This macro will be replaced during the run time by WordPress with appropriate random posts which is produced by predefined PHP function for that short code.
Can shortcodes have parameter?
Yes short codes can have parameters as well. For example lets talk about our random post short code. For this shortcode we can have a parameter which defines the number of posts to display. e.g. [random-post posts=”4″].
Can shortcodes have multiple parameters?
Yes a shortcode can have multiple parameters. For example if our randop posts shortcode case we can define number of posts to display as well as we can also define the category of the posts. e.g. [random-post posts=”6″ category=”technology”].
Can a shortcode have heading?
Yes a shortcode can have heading for it. For example if you want to display the heading for the random posts then we can have something similar to: [random-post posts=”4″] Some Random Posts [/random-post]
Why Should You Use Shortcodes?
One of the biggest benefit of shortcode is it helps in minimizing codes, and increases the re-usability functionality. For example when we write content for our website, chances are more that we may use the same HTML and CSS codes several times to include some special functionality in some specific pages and posts. In this case if we create and use short code then we can resue the code without rewriting the same codes every time, hence it saves our time and avoid any mistakes.
Some Built-In Shortcodes Provided By WordPress
For more detail about these built-in shortcodes you can click on the respective shortcode links. I hope this post was useful for those who are new to WordPress and shortcodes. In my next post I will write a simple function/plugin for this random post short code which will help all to develop plugins with shortcodes.