Date archives "July 2017"

How to use WordPress shortcodes inside widgets, footers, and more.

WordPress shortcodes are a handy way to extend the functionality of what’s possible within the WordPress editor that are aliased to short strings of text that look [like-this].

There are a lot of things a shortcode can be used for, but typically it comes down to two main use cases:

  1. Custom Embeds — Plugins that allow you to embed things like contact forms or a Google map will often use shortcodes so a user can decide exactly where to put it on their site, or even stick it inside a post.
  2. Dynamic Content — Sometimes if something should be dynamic on the page, a shortcode can provide an easy way to do that. If someone wanted to show the current year as in some copyright information on a site for example, they could register a shortcode called “[current_year]” so it would show up on the site as “2024” without having to go in and edit the text every year.

Using shortcodes inside widgets

By default, WordPress will only look for shortcodes inside post and page content. But if you’d like to add a shortcode inside a footer, or sidebar, or any other text widget you can enable shortcodes in widgets with this filter:

add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');
Using shortcodes in text widgets

Using shortcodes inside comments

You can also do this for WordPress comments. But keep in mind this filter is for the comment functionality native to core. Third party commenting systems like Disqus will not be impacted by this.

add_filter( 'comment_text', 'shortcode_unautop');
add_filter( 'comment_text', 'do_shortcode' );
Use shortcodes in comments

Using shortcodes in excerpts 

Excerpts are an optional summary or description of a post. It’s the text that goes before the “Read more” link in the archive and blogroll pages of most themes.

add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode');
Use shortcodes inside excerpts

Using shortcodes inside category, tag, and taxonomy descriptions

Not every theme using category and tag descriptions. But if yours does, and you want to use shortcodes in it, then use this code snippet:

add_filter( 'term_description', 'shortcode_unautop');
add_filter( 'term_description', 'do_shortcode' );
Use shortcodes in WordPress term descriptions

Putting it all together

If you want to use shortcodes in all of those places, you can combine all the snippets into one site specific plugin for your site, or just install this plugin:

Enjoy!