How to use shortcodes in Text Widget

Shortcodes can contain different functionalities which you can add to your WordPress website. Whether you have created one for yourself or it came with a theme or a plugin you’ve just installed, you will be probably using shortcodes all the time. Since you can really save time and benefit from them, there isn’t a reason not use shortcodes.

If you have just started building your WordPress website, you may have used shortcodes only in the Post editor. But, the truth is that you can use shortcodes practically anywhere.

For example, if you are using 5sec Google Maps PRO plugin which allows you to effortlessly create maps with numberless pins on it, you may want to insert the map via shortcode directly in your Text Widget and show your location on the sidebar instead of a single post. But if your theme doesn’t support shortcodes in widgets, you will need to enable the function by yourself and that’s what we’re going to show in this article.

If you need to use a shortcode in your PHP files (for example, if you want to run the code in your header.php file and run the shortcode wherever the header is present), you can do that easily by wrapping the code around it. But if you have tried to use a shortcode in your Text Widget like in the example we have mentioned above, you ended up disappointed because that simply doesn’t work and the widget displayed the shortcode instead its content. Unless your theme already has the feature registered, you will need to enable it by yourself.

So, if you want to be able to put a shortcode into a Text Widget and make it run, don’t worry; you can make it happen in a matter of seconds:

  1. Open functions.php
  2. Copy and paste the following two lines:
  3. add_filter( 'widget_text', 'shortcode_unautop');
    add_filter( 'widget_text', 'do_shortcode');
  4. Save changes

While it is actually enough to use only the second line which makes the shortcode work, you will want the first line in order to avoid WordPress’ automatic insertion of paragraph and break tags where you unconsciously create line breaks by formatting the text in a widget. Just in case you put a shortcode in a separate line, unautop will strip those paragraph tags and the shortcode will work like a charm.

Now you can navigate to Appearance -> Widgets and drag the Text Widget where you want to it be. After all this, when you write a shortcode directly into your Text Widget (which is registered on your site, of course) it will execute just like it was typed into the Post Editor.

Similarly, you can run PHP code from your Text Widget.

It was easy enough, wasn’t it? Enjoy your shortcodes.


This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *