How To Create Shortcodes In WordPress

In this post we are going to discuss how simple and easy to create a shortcodes for function in WordPress.

As a beginner many programmer think that it is tough approach to create own short codes in WordPress.

In this post we will see simple steps to create shortcodes in WordPress using WordPress add_shortcode() function which generate shortcodes for custom function.

Create Your Own WordPress Shortcodes

How To Create Shortcodes In WordPress

Basically first of all we see type of shortcodes in WordPress. Here we have categorised shortcodes and explain with example to get easily.

Shortcodes type are as follows:
i) Self Closing
ii) Enclosing
iii) Self Closing (With Parameters)
We discuss each shortcodes type in detail one by one. We are explaining shortcodes with social share and like link example.

1) Self closing shortcode

Self enclosing shortcode is the simplest shortcode in all types and easy to use and implement for new coders. To Create a Self closing shortcode put below snippet in WordPress function.php file.

<?php
function like_us_on_facebook()
{
 return '<a href="https://web.facebook.com/kumarvikashsingh324/">Like Us on Facebook</a>';
}
//add a shortcode for function
add_shortcode('likebutton','like_us_on_facebook');
?>

Usage:

In Template File

<?php 
echo do_shortcode('

[likebutton]’);
?>

In Post or Page

[likebutton]


Note:
With use of add_shortcode() function we can link any php function to our shortcode .
In other word create shortcode for our php function.
And can call shortcode anywhere.

2) Enclosing Shortcode

In enclosing shortcode we can pass the dynamic text on defining in page or post as shown below. To Create Enclosing shortcode put below snippet in WordPress function.php file.

<?php
function like_us_on_facebook ($attrs,$content)
{
 return '<a href="https://web.facebook.com/kumarvikashsingh324/">'.$content.'</a>';
}
//add a shortcode for function
add_shortcode('likebutton','like_us_on_facebook');
?>

Usage:

In Template File

<?php
echo do_shortcode('[likebutton]Like Us On Facebook[/likebutton]

'); ?>

In Post or Page

[likebutton]Like Us on Facebook[/likebutton]

3) Self Closing Shortcode (With Parameters)

In this type of shortcode we have an option to pass a dynamic parameters to call the desired output. Here three social links option is created and when we use its shortcode then we can pass a parameter for the link we want to display.

To Create Self Closing Shortcode (With Parameters) put below snippet in WordPress function.php file.

<?php
function like_us_on_social_channel ($links)
{
  extract( shortcode_atts( array(
        'links' => 'links',
        ), $links ) );

switch($links){

case 'facebook' : return '<a href="https://web.facebook.com/kumarvikashsingh324/">Like us on Facebook</a>';
break;

case 'twitter' : return '<a href="https://twitter.com/VikashkumrSingh">Follow on Twitter</a>';
break;

case 'gplus' : return '<a href="https://plus.google.com/u/0/+VikashKumarcreatingworld/posts">Be friend on Google Plus</a>';
break;

 
 }
}
//add a shortcode for function
add_shortcode('likebutton','like_us_on_social_channel');
?>

Usage:

In Template File

<?php echo do_shortcode('[likebutton links="facebook"]'); ?>
<?php echo do_shortcode('[likebutton links="twitter"]'); ?>
<?php echo do_shortcode('[likebutton links="gplus"]'); ?>

In Post Or Page

[likebutton links="facebook"]

[likebutton links="twitter"]

[likebutton links="gplus"]

Enable Shortcode in Text Widget

Above we have seen how to call or define shortcode in the template file, Post and Page. Now we will see how to use shortcode in text widget. To enable shortcode to use in-text widget we have to add some snippet in function.php file which is shown below.

Add below snippet to function.php

add_filter('widget_text', 'do_shortcode');

Shortcode in Text Widget: put the below shortcode in the text widget to display link.

[likebutton]

If you liked this article 🙂 please share it with your friends and colleagues 🙂 .If you have any query feel free to write in the comment box.

Leave a Comment