How to Create Custom Shortcode for WordPress

How to Create Custom Shortcode for WordPress

WordPress shortcodes introduced in its early version 2.5 before about six years ago and since that time, shortcodes are used to increase the functionality of WordPress.

The shortcodes are a particular tag that you can enter into a post or a page which gets replaced with different content when visitors are viewing the post or page on the website.

You’ve probably used shortcodes before, but did you know it’s pretty easy to create your own, with no plugins necessary?

In this tutorial, I will show you how to build some simple WordPress shortcodes for the subscription that will help you to create any functionality in future as you like.

You can directly create your shortcode function in the functions.php file or build a simple plugin. I'll show you both but at first, I go through the functions.php file.

Simple Facebook Follow ShortCode

In the beginner part of this tutorial, I will create a simple shortcode that will do the similar task in every time whenever you type it in the editor.

First, you need to create the callback function that will return the message what you want to show (in shortcodes we don’t echo anything, everything returned).

Just go to your functions.php file and paste the code snippet at bottommost.

function subscribe_link(){
    return 'Follow us on <a rel="nofollow nofollow" href="https://www.facebook.com/ThemeXpert/">Facebook</a>';
}
add_shortcode('subscribe', 'subscribe_link');

Test the ShortCode

Our shortcode is ready to use. Just add the code [subscribe] to your post or page wherever you want to show it.

See the image in below; it works correctly.

How To Add Parameters in Shortcode

Usually, you need to add a dynamic functionality in your shortcode and don't want to show the same content in every time.

As I am talking about subscription, there are lots of social networking sites in today's world; you may also want to get touched with your audience through many social sites. Therefore, I will show you how to add a parameter to the shortcode to change URL of social sites.

Again, just go to your functions.php file and add the following codes in there.

function subscribe_link_att($atts) {
    $default = array(
        'link' =&gt; '#',
    );
    $a = shortcode_atts($default, $atts);
    return 'Follow us on '.$a['link'];
}
add_shortcode('subscribe', 'subscribe_link_att');

Test ShortCode Parameteres

Now insert the following code to your post or page wherever you want to show it.

[subscribe link='https://www.facebook.com/ThemeXpert/']
[subscribe link='https://www.twitter.com/ThemeXpert/']

So, here is the image that shows it works correctly.

Working with Content

You have noticed that the previous example shows the direct link to social sites, but this process is not user-friendly. It looks good if it is possible to bind the links in HTML anchor tag.

Therefore, I will now show you how to working with contents within a shortcode.

It is quite same with the previous example just a tiny differences.

You need to add one extra parameter in the function and initially declare its value as null. And then call the do_shortcode function to use the contents.

See the following code.

function subscribe_link_att($atts, $content = null) {
    $default = array(
        'link' =&gt; '#',
    );
    $a = shortcode_atts($default, $atts);
    $content = do_shortcode($content);

    return 'Follow us on <a href="/.$a['link']." style="color: red">'.$content.'</a>';
}
add_shortcode('subscribe', 'subscribe_link_att');

Testing Content ShortCode

Insert the following code to your post or page.

[subscribe link='https://www.facebook.com/ThemeXpert/']Facebook[/subscribe]
[subscribe link='https://www.twitter.com/ThemeXpert/']Twitter[/subscribe]

Look at the image. I have add style in the anchor tag. Hence, the link text looks red.

ShortCode with a Plugin

If you don't want to customize your functions.php file, you can create a simple plugin to utilize this shortcode functionality.

To create a plugin just go to your plugin folder and make a folder named subscriptions and create a PHP file inside it with the same name, that means the plugin folder name and PHP file name will be same.

Now add the heading details of the WordPress plugin. You may follow the example in below:

/*
Plugin Name: Subscriptions
Plugin URI: http://example.com
Description: A very basic test plugin for subscriptions
Version: 1.0
Author: ThemeXpert
Author URI: http://example.com
License: GPL2
*/

Cut the code from your functions.php file which you added a few minutes ago and now insert here in the plugin file, below the heading information of plugins file.

Now you can see your plugin in the plugins directory. Just activate the plugin and follow the same process as before.

I hope you have enjoyed this whole tutorial on creating a unique shortcode for your WordPress website. Share your opinion through the comment section.

Rate this blog entry:
1
Top 10 Joomla Form Builder Extensions That Makes Y...
5+ Best Twitter Extension for Joomla