How To Use the WordPress do_shortcode Function Correctly
parallax background

How To Use the WordPress do_shortcode Function Correctly

WordPress .htaccess: Everything You Should Know About It
June 12, 2019
Fixed: The Uploaded File Exceeds the upload_max_filesizeDirective in php.ini
July 4, 2019

WordPress is loved by both professional and amateur webmasters because of the ease with which new functionality can be added with plugins and widgets. But there's a catch: not all plugins and widgets are able to insert content anywhere you want on a site, and they are often constrained to displaying custom content in pages, posts, or sidebars only.

Fortunately, you can use shortcodes in order to use custom pieces of code outside of a page, post, or sidebar – such as in the header or footer of your site. In most cases, plugins are developed to be used within WordPress’ visual editor, and widgets usually add code to the sidebar. With shortcodes, you can embed content directly into any WordPress template file, which will be displayed exactly where you place it. This article will focus on the do_shortcode function and how to use it to add new functionality anywhere you want.

What does this function do?


To understand what the WordPress do_shortcode function does, let's look at what a shortcode is. Shortcodes are small lines of text that can be added to your site which represent larger sections of code that are processed upon page load. When used, they invoke various functions that have various results (e.g. embedding a video or an interactive map). You can either use shortcodes written by you or you can use a plugin that automates the process and adds the shortcodes on its own.


The main functionality that WordPress do_shortcode adds to your site is embedding content easier into a theme file, whether it's in the header, footer, or somewhere within the post loop. Basically, you get to insert content on your blog in a dynamic manner. WordPress' do_shortcode function allows users to use shortcodes to personalize and improve their websites the way they want, by selecting which shortcode to appear where. It is important to mention that some WordPress plugins are used to generate shortcodes for different purposes, but you still need to implement these codes yourself. The only issue with WordPress is that you might not know where to place your shortcodes.

Another caveat is that when using the do_shortcode function, WordPress has to look through all possible shortcodes with RegEx to find the one that is being invoked, making this method somewhat slower than crafting a new, custom PHP function for what you want to achieve. However, as long as you don't go too wild with your shortcodes, the performance hit shouldn't be too bad.

How can you use it?

Normally, shortcodes can only be used in the WordPress editor. For instance, if you’d like to place a WordPress do_shortcode in the header section of your theme, you’ll have to do so manually.

Of course, you’ll have to be prepared to tweak some code by copying and pasting a line. The do_shortcode function can invoke any function you need to add to the website, as well as different features on different theme files, without the usual restrictions. The WordPress do_shortcode function looks something like this:

[?php echo do_shortcode("[shortcode]"); ?>

When should you use it?

Don’t use the do_shortcode function if you only want to alter HTML markups. You should use it when you want to:

  • Use shortcodes to load dynamic content of any type
  • Insert the Google Ads tracking code in different parts of your site
  • Insert a Google Map, contact form, or other widget content somewhere suitable

The 3-step guide to using the do_shortcode function

You can learn how to use the WordPress do_shortcode function very quickly. Follow these simple steps to use any shortcode on your site, using the header location as an example:

1 - choose the shortcode you want

As mentioned before, when you use the WordPress do_shortcode function you can invoke anything you want. You need to research the different shortcodes available to you with your theme, plugins, and widgets,and then choose the one that you’d like to add to your site. For example, the shortcode for a specific Contact Form 7 form might be
[contact-form-7 id="7"]

2 - find the file you want to add your shortcode in


Once you’ve found the exact do_shortcode function that you want to call, you need to find the header.php file in WordPress. The easiest way is to use WordPress' built-in theme file editor (Appearance > Editor).

3 - add the shortcode in this file


The theme you are using might alter where you find the header.php file or how it looks . Normally, the code in the header.php file contains multiple div sections. These sections are responsible for how your website looks. The do_shortcode function should be added within the div that is linked to the location where you want the function to appear. Here’s an example of using do_shortcode to add the Contact Form 7 on your site:


Other tips you should know about

  • Don’t abuse shortcodes by over-using them. Shortcodes are difficult to fix when they get broken, so try to keep their number to a minimum. WordPress' do_shortcode is very convenient to use, but be careful not to overwhelm your website. Some themes have more than 200+ shortcodes, and that’s not always the ideal situation.
  • Future-proof the shortcodes when you add them. That is, make sure that the shortcodes you add won’t become obsolete or broken in the future. Document which ones have been added so that you can keep track of them and double-check they still work when your theme or plugins are updated.
  • Learn how to search for shortcodes within your website. If you don’t know how to search for do_shortcode functions, it will be ten times more difficult to modify them if they are broken. The easiest way is to navigate to your theme’s folder in /wp-content/themes/your-theme-name/ and start your search from there.

Final thoughts

Once you understand the usefulness of the WordPress do_shortcode function, it will be difficult to stay away from using it.This short tutorial should have answered all of your questions regarding how to use WordPress shortcodes in any section of your site, and how to keep them working despite changes to your theme, plugins, or the core WordPress codebase.

If you enjoyed reading this article on how to use the WordPress do_shortcode, you should check out this one about how to make fewer http requests.

We also wrote about a few related subjects like how to remove proudly powered by WordPress, how to reset WordPress, how to change fonts in WordPress, WordPress favicon, how to duplicate a page in WordPress and how to get a WordPress mega menu.

Albert Ślusarczyk
Albert Ślusarczyk
As the co-creator of Be Theme, I am a strong believer in designing with care and patience. I pour my energy, time & knowledge into perfecting the theme for our 230,000+ customers.