How to Create Shortcode in WordPress

How To Create Shortcode In Wordpress

Create Shortcode in WordPress

In WordPress, the shortcode API works in a very simple way. If you want to create shortcode in wordpress then just follow these instructions carefully.
In order to create shortcode, first we need to create a callback function that can run anytime when the shortcode is used. Then, we need to bind that particular function to a specific shortcode that we can use anytime. We have to put all these codes in functions.php file.

Syntax:

add_shortcode( $tag , $func );

where;

$tag: It is the shortcode tag searched in post content.

$func: It is the hook to run when shortcode is found.

Example:

In this example, we want to create a shortcode that will generate some content every time when we type [testcontent] into an editor. First we have to create the callback function so that it will return our content.

callback function

After that, we need to add shortcode by using the add_shortcode() in functions.php file or other file that is being included. In add_shortcode(), we just need to pass two arguments, one will be the name of the shorcode that we want to use and other will be the name of the functions that we want to attach.

callback function

And finally, the code will look like as below in screenshot:

callback function