Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Use Shortcodes in Your WordPress Themes

Do you want to use shortcodes inside your WordPress theme?

Normally, shortcodes are used inside the content areas like posts, pages, or sidebar widgets. However, sometimes you may want to add a shortcode inside your WordPress theme.

In this article, we’ll show you how to easily add any shortcodes to your WordPress theme.

Easily use shortcodes in WordPress themes

What is a Shortcode in WordPress

A shortcode in WordPress is a special tag that allows you to add dynamic elements inside content areas like posts, pages, widgets, and more.

It is used as a text string inside square brackets. They typically look like this:

[gallery]

What do we mean by “dynamic elements”? A shortcode can be used to add all kinds of features to your WordPress site, such as image galleries, forms, social media feeds, and much more.

WordPress comes with a few default shortcodes and many popular WordPress plugins also use shortcodes.

For instance, WPForms has easy-to-use blocks, but it also provides shortcodes that you can use to add forms anywhere on your site.

WPForms shortcode

To learn more about shortcodes, see our complete guide on how to add a shortcode in WordPress.

Why and When to Add Shortcodes in Your WordPress Theme?

Shortcodes are normally used inside content areas of your WordPress website. You can easily add them from inside the WordPress admin area while editing your content.

For instance, you can add shortcode using the block editor in WordPress.

Adding a shortcode block in WordPress

However, some advanced users may want to use a shortcode inside their WordPress theme files. Now, if you added the shortcode in the same format as you use in content areas, then it will not work.

Shortcode not executed

Instead of seeing the output of your shortcode, you’ll see the shortcode itself printed on the screen.

The reason for this is that WordPress does not execute shortcodes inside theme template files. To run the shortcode, you’ll need to tell WordPress explicitly by using the do_shortcode function.

Here is a sample code on how to use it in your WordPress theme files.

echo do_shortcode('[gallery]');

Don’t forget to replace ‘gallery’ with the shortcode that you want to add.

Additionally, the syntax of this code will change a little bit if you wanted to add a shortcode with additional parameters.

Following is an example of the WPForms shortcode which has the form ID as an additional parameter to the shortcode.

echo do_shortcode("[wpforms id='147']"); 

What to do when do_shortcode is not working in WordPress?

Most shortcodes depend on WordPress plugins or some extra code on your website.

If the do_shortcode function is not working on your website, then make sure that the plugin providing the shortcode is installed and activated.

Make sure plugin providing the shortcode is activated

Similarly, if the shortcode is provided by your WordPress theme or some other custom code snippet, then make sure that it is available for use.

An easy way to troubleshoot and fix these issues is by using the shortcode_exists() function. This function simply checks the availability of a shortcode.

Here is an example of how you can use it with the do_shortcode() function.

if ( shortcode_exists( 'wpforms' ))  {
  echo do_shortcode("[wpforms id='147']"); 
}

If you are still unable to see the shortcode output on your website, then try clearing WordPress cache to make sure you’re not viewing an outdated version of your website.

We hope this tutorial helped you learn how to use shortcodes in your WordPress themes. You may also want to see these other useful WordPress tips, tricks, and hacks or check out these custom code tricks for functions file in WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

25 CommentsLeave a Reply

  1. I deleted contact form 7 and it is giving error like this [contact-form-7 404 “Not Found”] I don’t want to use the form again but want to remove this error I don’t know where to find that code or shortcode in template file

  2. hi,

    I have written a shortcode of a gallery plugin in wordpress visual editor, but it is not executing the shortcode, instead it displays the shortcode as it is written.

    like if I write [test attr=’hello’], it echoes the same on webpage, not executing.

    Thanks.

  3. Hello, I have two wordpress blogs on multisite.
    Do you know, how to use the shortcode on the website 1 from the website 2 ?
    Thx

  4. Sorry here is the code:

    CODE: ——————————————————————————————

    echo do_shortcode(“[tabset tab1=”tab 1 title” tab2=”tab 2 title”]
    [tab]tab 1 content[/tab]

    [tab]tab 2 content[/tab]
    [/tabset]”);

    —————————————————————————————————-

  5. That’s good information. I’ve been searching for a way to simply add these, I appreciate it.

  6. Great info. Thanks! However, it’s not working for me in WordPress 3.5.1 using the Avada theme.

    Do you need to add anything to functions.php for the code above to work? Could this be a theme-specific issue? Thanks, just learning wordpress templating.

  7. This is a great little tip. How would this work if your content needs to be wrapped in a shortcode?

    Like [shortcode-name]Content here[/shortcode-name].

    • Tyron,
      This a late answer as I’m just seeing it, but to include wrapped content, you could do the following:

      echo do_shortcode(‘[example_shortcode]’.$text_to_be_wrapped_in_shortcode.'[/example_shortcode]’);

      Cheers,
      J

  8. yes..i am also interested to know ..where to write the code. if there will be example that wud be great.

  9. Hi boys
    Where do you add the code…

    What file do you add it to and where abouts in the file?

    My theme already uses shortcodes but it would still be interesting to know.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.