Aug
30
2018

What Shortcodes are in 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.

Why ShortCode used in WordPress?

The basic reason to use Shortcode in WordPress is “Shortcodes are Magical…!!!”. In a very short time we will come to know this how magical the shortcodes are and will definetly find it interesting. Shortcodes are used to increase the functionality of WordPress.Remember that shortcodes should be created for content and functionality that you use frequently. The whole point of using shortcodes is to save someone time. If you are only going to use something once, there is not much point in creating a shortcode for it.

There are generally 3 ways one can create Shortcodes in WordPress, which are as follows :-

1. Creating a Shortcode Without Attributes – A Basic Example

2. Creating a Shortcode With Attributes

3. Creating a ShortCode with a Plugin

This tutorial covers the following things :-

1.   How to build WordPress Shortcodes Without Attributes and
2.   To make you understand the Structure of the shortcode and where to add the Shortcode .

So be ready and lets go ahead and make our hands a bit dirty with code and have some fun by lookig at how magical the shortcodes are!!

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).

1.   The Structure of Shortcode is as follows :-

// Create Shortcode
/**
*This Method is useful for creating the custom shortcode.
* @return string
**/
function custom_shortcode() {
    return statement;
}
/**
* This method is used to add the hook for the shortcode tag.
*@param string $tag give the name to the shortcode as per it's functionality
*@param string $func name of the function that you have created
**/
add_shortcode( $tag, $func );

2.   Where to Add the Shortcode?

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

3. Creating a Shortcode Without Attributes – A Basic Example

This example is for bringing the copyright text in a website. As copyright text is there in almost all the site’s the problem is that we have to change copyright text manually as the year changes but now as we have this shortcode we overcome this problem of doing this change manually.

// Create Shortcode for adding the CopyRight Text with year.
/**
* This Method is useful for creating the custom shortcode of the copyright text with year.
* @return string
**/
function copyright_text_year() {
$year = date('Y');
return 'Copyright © '.$year.' Mobio Solutions. All rights reserved.';
}
/**
* This method is used to add the hook for the shortcode tag.
*@param string $tag CopyrightTextWithYear. Here just keep a note that use this name without underscore,space and hypen otherwise it won't work.
*@param string $func copyright_text_year
**/
add_shortcode( 'CopyrightTextWithYear', 'copyright_text_year' );

Those of you who have no coding experience may find the above code a little daunting, however it is easy to understand once you break the code down line by line.

The first thing we do is add a comment above our function. This will help us quickly see what our function is for when we view the code at a later date. i,e.

// Create Shortcode for adding the CopyRight Text with year.

We then define our function. I like to use names that are self-explanatory, so I have called my function “copyright_text_year()”. i,e.

function copyright_text_year() {

Next, we define year variable and assign it the current year’s value using the php’s date function. The return statement will display the current year whenever it is called. It also stores the current year (as opposed to echo, which will print it but not store it). i,e.

$year = date('Y');
return return 'Copyright © '.$year.' Mobio Solutions. All rights reserved.';

The function is then closed. i,e.

}

We then define the shortcode itself using the add_shortcode function. The first variable specified defines the name of the shortcode that you want to use and the second variable calls our function (i.e. the one we defined above). i,e.

add_shortcode( 'CopyrightTextWithYear', 'copyright_text_year' );

Then add the shortcode [CopyrightTextWithYear] to the page or post of your choice as shown in the screenshot below.

Output of this :

Cheers!! You have successfully created your very first,easy and userful Shortcode.

In our next blog tutorial we will explain How to create shortcode with Attributes and how to create shortcode with plugin till then Stay Tuned!! 😉

Happy WordPressing!!! 🙂