Home » Source Book » How to Use do_shortcode in WordPress

How to Use do_shortcode in WordPress

Last updated on May 30, 2020 by

Table of Contents
1. What is Shortcode
2. How to Use do_shortcode() in WordPress
3. Add Shortcode in Page or Posts ( For Classic Editor)
4. Add Shortcode in Page or Posts ( For Gutenberg)
5. Add Shortcode in Sidebar Widgets
6. Enable Shortcode in Text Widgets
7. Add Shortcode in Template

We can easily extend the site functionality without repeating ourselves with the help of a shortcode. Nowadays, most of the plugins provide the shortcode functionality for better reusability. So the question is how to execute/run shortcodes? A very simple answer is, with the help of do_shortcode() function of WordPress. So, let’s start by exploring the basics of shortcodes!

do_shortcode() accepts two parameters. The first parameter is the name of the shortcode and it’s required. The second parameter is a boolean(true/false) to skip HTML elements or not and it’s optional.

Syntax: do_shortcode( 'name_of_shortcode', true/false value to ignore html or not );

The most common use of the do_shortcode() is the following:

echo do_shortcode('[name_of_shortcode]');

01 What is Shortcode

The shortcode definition varies in different people. We define it as like shortcode is a WordPress feature that helps you to overcome your repetitive task with very little effort.

ScratchCode’s simple definition: Shortcode is a short name of your bunch of a function’s code which allows you to execute anywhere in WordPress by just calling that short name.

02 How to Use do_shortcode() in WordPress

do_shortcode() function will search the shortcode and execute it. A do_shortcode() function is used to run/execute the user-defined or built-in shortcode. A shortcode is written inside two square brackets, For example, the "[sc_show_gallery]". As we have seen above that shortcode written inside two square brackets so here we have to use two square brackets while executing. Let’s add shortcode with the help of do_shortcode() but before that let’s assume you want to include a contact form 7’s shortcode into your post, page, template, etc as below.

echo do_shortcode( '[contact-form-7 id="91" title="Contact Us"]' );

do_shortcode() is by default enable with the_content() function that means whenever you find editor like “Old Classic Editor”or “Gutenberg” then it will automatically execute the shortcodes. You just need to copy-paste your shortcode. Let’s see some examples below.

03 Add Shortcode in Page or Posts ( For Classic Editor)

Go to your desire page or post and edit it. Now, you just need to copy-paste your shortcode inside the editor and you’re done. Check the below screenshot.

add shortcode in classic editor in WordPress
Figure 1: Add Shortcode in classic Editor

04 Add Shortcode in Page or Posts ( For Gutenberg)

Most of the people hate new editor in WordPress and they still choose to stick with Old Editor but honestly, we loved it. How people can reject such a cool Editor without using it. Well, It depends on people’s mentality so just leave it. Let’s back on track.

Go to your desire page or post and edit it. Then, you need to click on the add block button to insert a shortcode block like shown in below screenshot.

Add shortcode in Gutenberg Step 1
Figure 2: Add Shortcode in Gutenberg Step-1
Add shortcode in Gutenberg Step 2
Figure 3: Add Shortcode in Gutenberg Step-2

05 Add Shortcode in Sidebar Widgets

We can also run shortcodes in WordPress’s sidebar widgets. Go to the Appearance » Customize or Appearance » Widget and add the “Text” widget to a sidebar.

Then just add the shortcode inside Editor of “Text” widget as shown in below screenshot.

Add shortcode in sidebar in WordPress
Figure 4: Add Shortcode in Sidebar Widget

06 Enable Shortcode in Text Widgets

By default, shortcodes are not allowed to be executed in a “Custom HTML widget. To enable it, you just need to add the following code to your theme’s functions.php file.

add_filter( 'widget_text', 'do_shortcode' );

Then, you can simply add a “Custom HTML” widget to your sidebar and add your shortcode inside it as follow

Enable shortcode in widgets
Figure 5: Enable shortcode in Widgets

07 Add Shortcode in Template

To add the shortcode in the template you need to use do_shortcode() function. Let’s assume you want to add [sc_related_posts] shortcode into your “Contact Us” template, then template will look like below.

<?php 

/*
 * Template Name: Contact Us
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

get_header();

// Rest of Code

echo do_shortcode( '[sc_related_posts]' );

// Rest of Code

get_footer();

That’s it for now. We hope this article helped you to learn a function do_shortcode() in WordPress.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂

Leave a Comment