Enabling Shortcodes in Text Widgets

As you saw in Chapter 4, shortcodes are bracketed placeholders you can use while editing your content that will be replaced with some other, more complicated content (like an embedded video or image gallery) when your post is displayed to visitors. Unfortunately, shortcodes are processed only in post/page content. You can use them in text widgets, however, if you enable theme in the theme functions as shown in Listing 6-39.

Listing 6-39. Enabling shortcode parsing in text widgets add_filter('widget_text', 'shortcode_unautop'); add_filter('widget_text', 'do_shortcode');

The first line ensures that a shortcode placed by itself on its own line won't be surrounded by paragraph tags. The second line actually parses the shortcode. Figure 6-18 shows how the shortcode could be used in the widget.

Figure 6-18. A shortcode in a text widget.
0 0

Post a comment