WordPress shortcodes are small, one word keywords enclosed in [square brackets] that allow you to add functionality to WordPress. By default, you can use shortcodes in Posts and Pages, but not Widgets. Some users may want use a Widget that uses a shortcode to add a gallery or other macro into the sidebar. For more information on using Widgets, please see our WordPress Education Channel: Introduction to Plugins and Widgets.

How WordPress Handles Shortcodes in Widgets by Default

First, we'll see the default WordPress behavior when we use the gallery shortcode in a Text Widget. 

  1. Log into the WordPress Dashboard.
  2. Click on Appearance > Widgets
  3. Drag a Text Widget Into the Sidebar (on the right side of the page)
  4. Give the Widget a Title and type this shortcode into the text area of the Widget:[gallery]
  5. Save and close the Widget

    wp-text-widget


Here's what we see by default:


wp-widget-shortcode-before

Well, that's not very exciting is it? The good news is that this is fairly easy to fix. You actually have two choices to enable shortcodes in widgets in WordPress:

  • Use the Shortcodes in Sidebar Widgets plugin for WordPress
  • Edit the functions.php file for your theme.

Using the Shortcodes in Sidebar Widgets plugin in WordPress

Installing and enabling this plugin is the preferred method for adding this capability in WordPress. With this plugin, even if you change your theme in the future, shortcodes in your WordPress sidebar will still be enabled.

  1. Since we're already in the WordPress dashboard, search for and install the Shortcodes in Sidebar Widgets plugin.

    wp-shortcodes-sidebar-widget

  2. Make sure you have enabled the Shortcodes in Sidebar Widgets plugin.
  3. Refresh the page with the sidebar in your WordPress site and you will now see that the shortcode is working:

    wp-widget-shortcode-after

Editing Your Theme's functions.php file

While the following method is not the recommended method, some people may want to know how to enable shortcodes in widgets by adding some code to their theme's functions.php.

The following method would need to be repeated for each theme if you decide to switch themes in the future. Before proceeding with the following instructions, you should first create a Child Theme for your preferred theme and activate the Child Theme.

  1. Using the cPanel File Manager, create a new file named functions.php inside your Child Theme directory:

    wp-new-filewp-new-functions-php-file

  2. Open this file for editing by selecting either Edit or Code Editor in the cPanel File Manager.

    wp-edit-functions-php

  3. If a notification appears regarding encoding, make sure utf-8 is selected and click the Edit button in that window.
  4. Input the following code in the functions.php file:

    <?php
    add_filter( 'widget_text', 'shortcode_unautop');
    add_filter('widget_text', 'do_shortcode');
    ?>

  5. Click the Save Changes button to save your changes, then click the Close button to close the file.
  6. Now shortcodes are enabled in Text Widgets for your WordPress site:

    wp-widget-shortcode-after_copy

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Did you find this article helpful?

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Need More Help?

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.
}