Ever wished you could use WordPress shortcodes directly in your theme template files or generally execute them via PHP? With the WordPress do_shortcode function, you can make your wish a reality — and we're going to show you how.

Though WordPress has started to move away from shortcodes with its shift toward the block editor (AKA Gutenberg), shortcodes still play an essential role in the WordPress ecosystem.

With just a set of brackets and some text — e.g. [yourshortcode] — you can embed content, perform functions, and accomplish lots of other tasks more efficiently.Grow Your Business With HubSpot's Tools for WordPress Websites

But if you've ever tried to add a shortcode directly to your theme's template files, you've probably figured out that this approach simply just won't work. The shortcode will display as plain text — it won't actually execute the shortcode and render the content that you want to display.

However, that doesn't mean you can't use shortcodes in your template files. With the WordPress do_shortcode function, you can easily insert any shortcode directly in any template file.

Keep reading to learn how it works and some other important best practices for using WordPress shortcodes in your template files.

How does WordPress do_shortcode work?

With the do_shortcode function, you can essentially wrap your shortcode in the do_shortcode PHP function and use that to add the shortcode directly to your site's template files.

You can put the shortcode anywhere on your site, including the following locations:

  • Header
  • Footer
  • Single templates (e.g. a single blog post)
  • Archive templates (e.g. the page that lists all of your blog posts)

To use it, all you need to do is add the shortcode inside the WordPress do_shortcode function like this:

<?php echo do_shortcode( '[your_actual_shortcode]'); ?>

You can then add this function directly to your template files. Or, you can use WordPress hooks to insert it at specific locations.

Not sure how it works? Let's look at an example.

WordPress do_shortcode Example

To help you understand how the do_shortcode function works, let's look at a real example.

Let's say you created a form with the WPForms plugin and now you want to embed that form directly in your theme's template files using do_shortcode.

Here's how to do it.

1. Backup your site and choose a child theme.

Because you'll be editing your template files directly in this example, it's essential to do two things before you start:

  1. Back up your WordPress site just in case anything goes wrong.
  2. Make sure to use a child theme. If you don't, your template changes will get overwritten the next time you update your theme. Read our full guide to WordPress child themes if you're not sure what to do here.

Once you've done that, you're ready to begin.

2. Prepare your shortcode.

To get started, you'll want to prepare the shortcode that you're going to use.

For our WPForms example, you can find the shortcode in the WPForms interface.

do_shortcode wordpress example

2. Edit the template file.

Next, you need to edit the template file where you want to include your shortcode. For this example, let's say that you want to add the form below every single blog post and page.

To accomplish that, you would need to find your theme's single.php template. To add the shortcode below the content, you would want to add the do_shortcode function below the the_content function in the template file.

You can make your edits via WordPress' built-in theme editor by going to Appearance → Theme Editor. Or, you can connect via an FTP client and edit the files that way.

3. Add the WordPress do_shortcode function.

To finish things out, just add the do_shortcode function with the shortcode inside at the proper location. Make sure to echo the do_shortcode function — otherwise, you won't see the output on the frontend.

do_shortcode wordpress function

Once you save your changes, you should see your shortcode's output at the location where you added the function. Here's what it looks like with our example and the default Twenty Twenty-One theme.

wordpress twenty twenty one theme

If everything is working fine, you're done. If you're having issues with WordPress do_shortcode not working, continue on to the next section for some troubleshooting steps.

Troubleshooting When WordPress do_shortcode Is Not Working

If you're having issues with WordPress do_shortcode not working, here are some basic troubleshooting steps to consider.

1. Echo the shortcode.

By itself, WordPress do_shortcode just returns a value. If you want to actually display the output of the shortcode, you need to echo the do_shortcode function.

If you notice in the WordPress do_shortcode example above, we make sure to always echo the do_shortcode function to display the value.

2. Double-check that you're using quotation marks properly.

If you experience a PHP error after adding a shortcode with do_shortcode, it might be an issue with the quotation marks that you're using.

Some shortcodes let you add parameters inside the shortcode, which can cause problems if you make a mistake with the quotation marks that you use.

For example, let's say you want to add a shortcode like this:

[contact-form-7 id="44" title="Main Contact Form"]

The shortcode already includes double quotation marks. So, if you try to use double quotation marks for the do_shortcode function, you'll likely trigger an error.

The solution? Just use single quotes for the PHP function.

Here's the wrong way to do it — notice how the shortcode is wrapped in double quotes outside:

echo do_shortcode("[contact-form-7 id="44" title="Main Contact Form"]");

Here's the correct way to do it — notice how the shortcode is now wrapped in single quotes, while there are still double quotes inside the shortcode:

echo do_shortcode('[contact-form-7 id="44" title="Main Contact Form"]');

3. Confirm that you are using the correct shortcode.

This might seem obvious, but it can actually be a little tricky. Some plugins will use the exact same shortcode syntax but implement things slightly differently so that it's not really a WordPress shortcode.

To you, it looks exactly like a shortcode, so you think that it should work fine in WordPress do_shortcode. But to your WordPress site, it's not actually a shortcode, which is why do_shortcode isn't working..

Are there any risks with using WordPress do_shortcode?

The WordPress do_shortcode function can be really handy when you only have a shortcode and you need some way to add it to a template or PHP code.

With that being said, you should generally try to avoid using it when possible.

Why should you avoid it?

The issue is mainly to do with performance. It's not that using do_shortcode will automatically slow down your site. In most cases, it's totally fine and you won't notice a difference.

However, it's not the optimal technique from a performance perspective. So if you can easily avoid it, it's best to use a different approach (which we'll discuss below).

Here's why. When you use do_shortcode, you're essentially forcing your site to run through all of the shortcodes that exist on your site just to execute the one shortcode that you need. On a technical level, WordPress does this by using a large regex every time the do_shortcode function is called.

That includes all of the default WordPress shortcodes, all of your theme's shortcodes (if applicable), and all of the shortcodes from plugins that you're using.

Obviously, this isn't as efficient as using a PHP function. Is it going to slow your site to a crawl? No, it's not a huge deal. But whenever possible, it's good to use a different approach.

So, what are your other options? Let's go through a few.

WordPress do_shortcode Alternatives

1. Use the PHP Function if provided

Nowadays, many plugins give you both a shortcode and a PHP function as embed options. When you have both of these options, you should always choose to use the PHP function instead of the WordPress do_shortcode function. There's really no added complexity and it will improve the performance and efficiency of your site.

2. Find the shortcode’s callback function.

If the plugin doesn't provide you with a PHP function but you'd still like to avoid using the WordPress do_shortcode function for performance reasons, you do still have some other options.

However, these solutions can start to get a little complicated. If you're not a very technical person, you might want to just stick with do_shortcode for simplicity's sake.

The idea here is that instead of using the shortcode and do_shortcode, you can find the shortcode's callback function. This is the actual PHP function that is called when you use the shortcode.

You can then echo that function instead of do_shortcode.

However, there are two downsides to this:

  1. It can be tricky to find the callback function if you're not a very technical person.
  2. It can be really tricky if the callback is inside an object class. You might not be able to access the callback directly, which necessitates creating a custom callback utility and is beyond the scope of this article.

For a lot of simpler plugins, you can find the callback function without too much difficulty. For this example, let's say you want to use the shortcode from the Current Year and Copyright Shortcode plugin.

Here's the simplest way to do this:

  1. Download the plugin and extract the zip file so you can easily access all of the plugin's files.
  2. Use software that will let you run a search on all of the files in that folder. Notepad ++ is a solid free option that makes it easy to do this.
  3. Search for the shortcode that you were planning to use in the WordPress do_shortcode function.

You should see something like this:

add_shortcode( 'cy', 'jr_cy_cy' );

do_shortcode wordpress plugin

In this example:

  • [cy] is the shortcode
  • jr_cy_cy() is the actual function that the shortcode invokes

With do_shortcode, you would implement it like this:

echo do_shortcode( '[cy]' );

But now that you have the callback function, you can use the function directly like this:

echo jr_cy_cy();

You get the exact same effect, but you've reduced the work that your site has to do.

Getting Started With WordPress do_shortcode

The WordPress do_shortcode function makes it easy to include shortcodes directly in your theme's template files. All you need to do is include the shortcode inside of the do_shortcode function and echo the function in the template location where you want the shortcode to appear.

You'll also want to pay special attention to the quotation marks that you use, as the difference between single quotes and double quotes is very important when using do_shortcode.

While one of the attractive things about do_shortcode is its simplicity, it's generally a good practice to avoid using WordPress do_shortcode when easily possible because it's not as performance-friendly as calling a PHP function directly.

If a plugin gives you both a shortcode and a PHP function, it's always better to use the PHP function over the shortcode. And, even if the plugin doesn't provide a PHP function, you might be able to easily find the callback function associated with the shortcode and call that function directly.

do_shortcode is generally the easiest way to work with shortcodes in template files. So, if it's the only method you can get working, stick with do_shortcode.

Give it a try and enjoy the added flexibility for working with your theme's template files.Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

 Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Originally published Jan 28, 2022 7:00:00 AM, updated January 28 2022

Topics:

WordPress Website