Shortcodes are one of the most amazing features of WordPress and were introduced in version 2.5. Without knowledge of programming one can publish dynamic content using macros. On insertion of a shortcode, WordPress replaces the page with another dynamic content. First WordPress identifies the macro which is in square bracket ([ ]). As the next step it is replaced by a different content. The new content is the product of a PHP function.
The application of this feature is quite simple and easy. Suppose we want WordPress to show the latest titles that are added in a particular post. Then we can mention it as
The shortcode can be made more specific by inserting the number of new titles that we want to show. Suppose we want to see the ten latest titles. Then we can mention it as
1 [recent-titles titles= “10”]
The next innovation can be providing a heading for the list of latest titles:
1 [recent-titles titles= “10”] Posts Heading[/recent-titles]
Here, our first task is to create the code for the shortcode
The creation of this type of code is very simple and can be done easily without PHP knowledge. After creating the function, it will be identified by WordPress as the shortcode. It is to be registered by providing a unique name. Finally the registration function is to be attached to WordPress action.
When we have a shortcode, the same can be replaced by a piece of code which is called the callback function. The callback function gives back a string instead of printing anything. In the above example the string will be in response to our query to the database to get the latest topics. It is all the more important to give unique name for shortcodes to avoid disputes. WordPress shortcodes ensure security of the content in a post and protect the content from attacks.
Leave a Comment