Adding Shortcode in WordPress Guide

Rodger Davies

When you are building a website on WordPress, you need to make sure that it looks great and works properly. One of the best ways to improve how the site works is to add shortcodes to WordPress. These shortcodes can add all kinds of features such as image galleries and contact forms to your site. These make your site stand out without having to do too much coding. In this guide, we are going to teach you how to add shortcode in WordPress theme files and more. Keep reading to find out how this can work for you.

What Is WordPress Shortcode?

Before you learn how to create shortcode WordPress, you need to know a bit more about what shortcode is. Essentially, these are shortcuts that can help you to add dynamic content to your site – sounds great right? With a shortcode, you can add contact forms to your site amongst other things and generally just make it more user friendly. If you aren’t already using shortcodes then now is the time to really get the hang of them.

Usually, you’ll find your shortcode displayed within square brackets so they are reasonably easy to identify. Shortcodes help to add features without having to know too much about coding. This can come in very handy for beginners on WordPress and they can help you turn a dull site into one that is going to make a big impact.

How To Add Shortcode In WordPress

There are a few different places that you can add shortcode in WordPress depending on what you are trying to achieve. One of the most common places that shortcode can work effectively is in your posts and pages. Don’t worry if you don’t know how to do this because we are going to take you through it below.

The first step to adding shortcode in WordPress pages or posts is to edit the page or post and then add a block. Here, you will need to add a shortcode block and then enter the shortcode that you want. Depending on the plugin that you are using, you will usually find that the shortcode will be provided. Make sure to find out exactly what the code is before entering it to make sure that everything goes smoothly.

Adding Shortcode To Sidebar Widgets

Posts and pages are not the only places that you can add shortcode, in fact, it can be very useful to add them to a sidebar widget. This can be easily done by visiting your ‘appearance’ toolbar and then ‘widgets’. The next step is to add the ‘Text’ widget to your existing sidebar. Here, you will be able to add your shortcode into the text area and this should add the dynamic feature. It is really important that you remember to save here or else you might lose your new feature.

Using The Classic Editor?

If you are using the classic editor in WordPress then you might have to go about adding your shortcode a different way. You will need to paste the shortcode into the content editor on the page that you are hoping to update. Here, it is important to remember to add the shortcode on its own line and save your changes after. Adding Shortcode to WordPress Theme Files While most people use shortcode in their posts and pages, others find that it can be useful to use a shortcode in a WordPress theme file. To achieve this, you will need to know how to edit your theme files which can be a little tricky. Make sure that you know how to do this effectively before trying to add your shortcode in.

Once you know how to do this, you can add your shortcode. The code that you’ll need will look
something like the following with your shortcode added in.

<?php echo do_shortcode("[your_shortcode]"); ?>
This should then add the shortcode to your theme once WordPress has located it.

Want To Create Your Own?

While you can usually find a shortcode when you install a new plugin, it is also possible to create a custom shortcode in WordPress. This is something which only those who have some coding experience can do easily and so make sure that you know what you are doing before you go ahead with it.

There is a basic template that you can follow that is in PHP code format. This should look something like the code below:

// function that runs when shortcode is called
function wpb_demo_shortcode() {

// Things that you want to do.
$message = 'Hello world!';

// Output needs to be return
return $message;
}
// register shortcode
add_shortcode('greeting', 'wpb_demo_shortcode');

You will need to make sure that you have created a function first and then created the shortcode. Then, you can go about telling WordPress to run your shortcode and hopefully this should fall into place. It is important to remember here that you should be adding in your own code to this template or else it isn’t going to achieve what you set out to do. Creating your own code can be tricky so if you are not experienced then you should maybe start off with shortcode created by plugins and then try it out yourself once you know a bit more about it.

Key Points

Shortcode can massively save time when you are trying to improve your WordPress site and so it is a
good skill to get the hang of early on. Usually, when you have a plugin, you will get the shortcode and so you can use this following the method that we set out in this guide. Make sure to follow everything that we have discussed carefully and ensure that you do not miss out on any of the important steps. Whether you are adding shortcode to your posts or your widgets, you should be able to get the hang of this in no time at all.

Rodger Davies

Author Rodger Davies

Rodger spent ten years working in-house for a global chemical manufacturer, in roles across marketing, design and web project management. Since starting work with Elementary in 2017, he has worked with a wide range of clients from various sectors.

More posts by Rodger Davies