Home » Source Book » How To Use do_shortcode In WordPress

How To Use do_shortcode In WordPress

Last updated on June 11, 2021 by

In this article, we will learn how to use do_shortcode in WordPress. We can easily extend the site functionality without repeating ourselves with the help of a shortcode. Nowadays, most plugins provide shortcode functionality for better reusability.

So the question is how to execute/run shortcodes? A very simple answer is, with the help of the do_shortcode() function of WordPress. So, let’s start by exploring the basics of shortcodes!

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

The WordPress’s 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 In WordPress

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 that 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 in WordPress. 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 an editor like “Old Classic Editor” or “Gutenberg” then it will automatically execute the shortcodes WordPress. 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 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 as shown in the 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 the 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.

Additionally, read our guide:

  1. How To Add Back Button In Elementor
  2. 403 Error When Updating In Elementor
  3. How To Add Multiple Post Types In Posts Widget In Elementor
  4. How to Add Products Per Page Dropdown in WooCommerce
  5. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  6. How to Create a Plugin in WordPress from Scratch
  7. How to Disable Admin Bar in WordPress Without Plugin
  8. How To Send Custom Emails in WordPress
  9. How to Allow Preview of Draft Post Without Login in WordPress
  10. Import Users From CSV In WordPress Programmatically
  11. Dynamically Populate A Select Field’s Choices In ACF
  12. How To Create Database Table In WordPress
  13. Difference Of require, include, require_once And include_once
  14. PHP: Get Day Name From Date
  15. PHP: Get Year From Date
  16. How To Update Pivot Table In Laravel

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