PHPMatters Help You Better Hosting Your PHP-based Sites
How to Create a Simple Shortcode in WordPress?

How to Create a Simple Shortcode in WordPress?

Shortcodes are one of the most useful features of WordPress, making inserting sentences or even PHP functions on a page or page easy. When you have created a shortcode, you only need to put some simple words in square brackets, and WordPress will automatically replace it with other dynamic contents when showing the shortcode in the front end.

For example, you may have used the [gallery] shortcode on a page. When you view the page on the front end, you can find that WordPress has replaced the shortcode with a gallery of images that you want it to display. There are several default shortcodes offered by WordPress, including [gallery], but the awesome thing is that the shortcode API allows you to reach many more purposes by creating custom shortcodes, such as embedding a Youtube video, showing latest tweets, and displaying recent posts.

Using shortcodes reduces the manual efforts of inserting complicated HTML and CSS into pages or posts. You do not need to remember each line of code carefully because shortcodes can handle the work for you. Now many people, especially beginners, may have the question of how to create a custom shortcode.

How to Create a Simple Shortcode Manually?

In fact, the task is easy if you only want to achieve something simple. This tutorial will show you the detailed steps to get the job done. But we have a suggestion that you should only create shortcodes for those frequently used functions but not the ones planned to use once only.

Step 1: Define the Function of the New Shortcode

First of all, you need to create a callback function that tells WordPress what to do when the shortcode is used. The function should be put into the functions.php file of your theme directly, or you can create a new file and include it in functions.php. The latter way is recommended when there are different shortcodes created. This requires little PHP knowledge.

To make an example, we will show you how to create a [twitter-subscribe] shortcode which encourages readers to subscribe to your blog updates. Open the functions.php file and add the code below in a new line. You can access the file by visiting Appearance > Editor and finding “Theme Functions”.

function twitter_subscribe_link() {  
    return 'Like this post? Subscribe to our updates <a href="">by Twitter</a>.';

With the line of code, we tell WordPress the following things.

function twitter_subscribe_link() {  

This defines the function. Because we want to display a subscription link for Twitter, we name it to be “twitter_subscribe_link”. The name can be whatever you want and it does not affect how the shortcode will function.

The message after “return” is what will display whenever the shortcode is called. This statement does not echo anything, but instead, it stores messages and returns them. “}” indicates the end of the code.

Step 2: Define the Shortcode and Hook into WordPress

As soon as the callback function is created, it’s time to define the shortcode itself and ties the function above with the shortcode. You can do this by adding a new line of code in the functions.php file or another file included.

add_shortcode('twitter_subscribe', 'twitter_subscribe_link');

The add_shortcode function comes with only 2 arguments which are easy to understand. The first one is the name of the shortcode which you will insert into posts and pages, and the other one is the name of the function you gave before. After you update the functions.php file, you have created a simple shortcode.

Step3: Test and Use the Shortcode

You can now put the shortcode [twitter_subscribe] shortcode anywhere you like, and then preview the post/page to see whether it functions as you expect. If there is anything wrong, go back to check the codes carefully.

Twitter Subscribe Shortcode

This is a simple example for displaying texts and links. You can also customize a shortcode to insert Adsense ads, make a subscription form, and do many more. Just doing more practices to master the function, you will benefit a lot from the flexibility.

How to Create a Shortcode by Using Shortcodes Ultimate Plugin?

Besides the manual way, you can also get the creation and use of shortcodes achieved by installing plugins. Here we would like to introduce Shortcodes Ultimate, one of the best in its kind that offers not only a shortcode creator, but also dozens of free shortcodes that can be used directly. The built-in shortcodes can meet the demands of most normal bloggers.

Create a shortcode

We will not discuss how to create a completely new shortcode in this plugin in detail, because the “Cheatsheet” of it includes detailed attributes and examples that you can customize directly. For example, if you would like to insert a Youtube video in a post, you can only customize the attributes of URL, Width, Height, Responsive, Autoplay and Class and then insert the shortcode in the post. No further job. Both simple and advanced shortcodes can be created in this way.

Youtube Shortcode

Take advantage of the examples

As is mentioned above, Shortcodes Ultimate comes with many pre-configured shortcodes that can be used directly. So if you combine the Cheatsheet and Examples features, you can easily customize some simple shortcodes to meet your own demands.

In the examples, there are basic and advanced shortcodes for buttons, animations, tabs, and many more. Taking adding a custom button in a post as an example, you need to go to Shortcodes > Examples, and click to open the “Unlimited buttons” tab.

Button Shortcode

In the pop-up screen, click on the “Get the code” button, and copy the shortcode for the button size, color, style, icon font and more.

Button Size

The last step is to combine all the attributes you selected together to get your custom shortcode which displays a special button as you want. The “Click me” text can be customized in your way, too.

The shortcode below is what we use to test the functionality. You can practice more and learn some skills by analyzing the shortcodes in the Cheatsheet with the ones in the Examples.

[su_button size=”3″ background=”#FF742F” style=”bubbles” icon=”icon: shopping-cart” url=””] Buy Now [/su_button]


Other WordPress Shortcode Plugins Worth a Try

If you do not like Shortcodes Ultimate, there are still some alternatives which you can have a try. Below are 3 leading ones which help much in enriching your WordPress site with easy-to-use shortcodes.

WordPress Shortcodes


A free plugin that offers beautiful and easy-to-handle shortcodes that enhance the functionality of layout, lists, buttons, tabs, message boxes, and more. The plugin comes with an intuitive editor and all parameters are documented in the editor for help.

WordPress Shortcodes

WP Canvas – Shortcodes


WP Canvas supports shortcodes for dozens of features. You are able to use it to simplify the creation and management of buttons, borders, notifications, Google maps, countdown, columns, tabs, social icons, and so on. It also offers detailed tutorials for beginners.

WP Canvas



This plugin is rated 4.7 out of 5 on It is created to offer an easy way for people to add shortcodes for any frequently used snippets. When the shortcodes are not needed anymore, you can disable them one by one, or choose to disable them globally.