How to Create Shortcode in WordPress

A SIMPLE SHORTCODE

The shortcode API works very simply: first you need to create a callback function that will run anytime the shortcode is used; then you need to tie that function to a specific shortcode making it ready for use. The code is frequently placed in the functions.php file, but if you plan on having a lot of shortcodes, it makes sense to create a separate file and include that file in your functions.php file.

In our first example we want to create a shortcode that will create some lorem ipsum every time we type [lorem] into the editor. First we need to create the callback function that will return the lorem ipsum (in shortcodes we don’t echo anything, everything is returned):

Step-1 : Register Shortcode 

W need to add this shortcode to WordPress using the add_shortcode function in either our functions.php file or a file that’s being included in it

Step-2 : Define Your Custom Function:

Custom function is a callback function where you can write any thing what you want to show

Step-3: Use Shortcode in WordPress

We need use WordPress built function do_shortcode  to echo content of shortcode in your php files.

That is all it takes to create a simple shortcode in WordPress.

 

 

ADDING PARAMETERS

we’ll create a shortcode to insert an image like this:

Next we need to create the callback function like below:

Finally the function returns the value we want, in this case the HTML code for our image combined with the width and height variables.
The only thing left to do is register this shortcode:

 

Leave a Reply

Your email address will not be published. Required fields are marked *