How to enable shortcodes in Wordpress widgets

Quickly gain this powerful feature in your Wordpress widgets

Shortcodes in Wordpress are small snippets of text that you can enter into a post or page body which trigger a larger function to run at that position. It’s the equivalent of inserting a program inline with your content and it’s useful in many scenarios. Wouldn’t it be nice to have this power in sidebar widgets as well?

A common case for a shortcode is to insert a contact form onto a page. If you’ve ever used the popular plugin Contact Form 7, you’re probably familiar with the process. It’s simple really. Once you create the contact form the plugin supplies you with a shortcode to insert onto any page or post you’d like it to render on.

Adding this to your page body

1_31.png

Results in this on the page

2_19.png

But what if you want to put that contact form into a sidebar widget? If you could use the same shortcode you’d be done in a snap, but unfortunately that functionality is not enabled in Wordpress by default. Thanks to Wordpress’s action hook system however, enabling it is not difficult.

To enable this functionality, you only need to add a new filter hook into your themes functions.php file. If you have the ability to do so, open up the functions.php file in your theme directory and add the following line to the top (after any comments) or bottom of the file:

add_filter('widget_text', 'do_shortcode');

If you don’t have the ability to access the functions.php file, or are just nervous you might break something, I’ve created a plugin that does the same thing which you can install and activate just like any other plugin. Once activated, just add a Text widget to your sidebar and use any shortcode inside of the widget body.

Plugin Download

Enable Widget Shortcodes Plugin

What’s wrong? The new clean desk test
Join the discussion
Be the first to comment on this article. Our Commenting Policies