Utilizing The Short Code API to enhance your capability to use Short Tags in WordPress

Jack Calder 10th Oct 2020

Getting To Know The Use of Short Code API to Enhance WordPress Capability

WordPress introduced the concept of shortcodes in version 2.5, and since then all of us have probably used them for different purposes. On its bare bones, shortcodes are nothing but a set of a bit of code that allows webmasters to embed some dynamic function in their plugins or themes without having any need to insert HTML code. They can be replaced with some other content. In simple words, they instruct WordPress to watch for the macro, which we insert inside the square brackets and then get it replaced with some other content; it could be a simple text, sentence, or output of a massive PHP function, depending upon what we instruct WordPress to do.

Why do we need to use shortcodes?

Shortcodes can be extremely useful for WordPress users to modify the look and feel of their website template and the content. They play an important role in developing engaging templates and bespoke content. One can use shortcodes to inculcate videos, images, text, buttons, maps, and so on, and thus make their content absolutely stand out. Shortcodes also help you to insert some special functionality on those areas of your content which are normally unreachable. For example, you can make use of a text widget to add testimonials right onto your sidebar, or you can add a call-to-action button in the header. This brings new opportunities for your website and gives you complete control over its behavior.

Short Code API

The shortcode API offers developers a smart way to create their own shortcodes for creating specialized content like contact forms, which can be further attached with specific pages by embedding a corresponding shortcode. The benefit of shortcode API is that you don't need to write a custom regular expression for each shortcode that you've included in your post because they are automatically detected by the WordPress, which further calls the corresponding function and replace the shortcode with the specific text.


As of now, you've gained a pretty good understanding of what WordPress shortcodes are and what they do. Now, let's move on to learn how to create your own shortcode.

1. Register the Handler Function

The shortcode WordPress API works very easily. All you need is just register your short code with the WordPress function called add_shortcode (). The function basically handles two variables: the name of the short code to be used and the callback function. The callback function requires one thing: the content which replaces the shortcode must be identified in the 'return' instruction.

Next, you need to choose between the following parameters, which can be tied with the specific callback function to make it ready for use. They are:

$atts - the parameter is used if we wish to have an array of attributes to be defined in the shortcode.

$content - if the shortcode is used in the enclosed form.

$tag - it's a shortcode tag mainly used for the shared callback functions

The follow API will inform you how to register the shortcode handler.


add_shortcode( 'customshortcode', 'custom_shortcode_handler' );


If the template tag the_content is found, then short code API will automatically parse  all the registered shortcodes i.e., custom shortcode. Next, the content and the attribute will get separated, parsed, and then passed on to the replaced shortcode handler.

Here, it's worth mentioning that any string which is returned by the handler  will replace the shortcode automatically wherever it's being placed.

2. Using Attributes for the Shortcodes

Below we have given a simple example of PHP snippet that will assist you in creating the shortcode.


</> //[newrule]function newrule_func( $atts ){ return "new and rule";}add_shortcode( 'foobar', 'newrule_func' );


Executing the above code will create a [newrule] shortcode, and will return the values: new and return. Now, look at the following code to know about the process of adding attributes for the shortcode.


[customshortcode new='rule” rule=”bing”]


Now, we will convert these attributes into an associative array just like this:


array( 'new' => 'rule', 'rule' => 'bing' )


Now you can see in the above function that array keys are attribute names and attribute values are the corresponding values. What we have done here, we have passed the array to the handler function that is $atts [0] containing the strings that matches the shortcode regex.

Our output will look like this:


function customShortCode() {
return ob_get_clean();



WordPress shortcodes are powerful function that allow you to create an exciting website easily and quickly. The make your content look more attractive and professional.

Authored By Jack Calder

Jack Calder is a expert professional with Markupcloud Ltd involved in various markup conversion services including the process of how to convert psd to html in an easy manner. Apart from this he also shares secret about web design & development.

Also on DiscussDesk