Run Shortcode on Widget Title

%%title%% %%sep%% %%sitename%%
Run Shortcode on Widget Title

Learn how to run shortcodes in widget titles in WordPress with a simple filter. Add custom formatting like bold text using shortcodes in widget titles for more flexibility.

You dropped a shortcode into a widget title and it printed as plain text, brackets and all. That is WordPress doing exactly what it was told: widget titles don’t run shortcodes out of the box. One filter changes that.

How to Enable Shortcodes in Widget Titles

Add this to your theme’s functions.php or a small custom plugin:

add_filter( 'widget_title', 'do_shortcode' );

That points WordPress’ built-in do_shortcode function at the title, so any shortcode you put there gets processed instead of printed raw.

Example: Adding a Bold Shortcode to Widget Title

Say you want to bold part of a title. First you need a shortcode to bold with, then you use it.

Step 1: Use the [b] Shortcode in Your Widget Title

In the title field, write:

Make my title [b]bold[/b]

For that to do anything, you have to define [b] yourself.

Step 2: Add a Shortcode to Make Text Bold

Register a function that wraps the enclosed text in <strong> tags:

/**
 * Shortcode to make content bold.
 *
 * This function takes the content passed between the shortcode tags and wraps it 
 * in HTML  tags, making the text bold. It can be used in widget titles or 
 * anywhere shortcodes are processed in WordPress.
 *
 * @param array $atts Attributes passed to the shortcode. Not used in this case.
 * @param string $content Content enclosed within the shortcode tags.
 * @return string The content wrapped in  tags for bold styling.
 */
function boldify( $atts, $content = "" ) {
    return "$content";
}

// Register the [b] shortcode, which makes text bold.
add_shortcode( 'b', 'boldify' );

// Ensure that shortcodes are processed in widget titles.
add_filter( 'widget_title', 'do_shortcode' );

add_shortcode ties [b] to the function, and do_shortcode on the filter makes it fire inside the title.

Sample Output

With the code in place and the shortcode in your title, you get:

Make my title bold

The word “bold” now renders bold, right there in the widget title.

One Honest Caveat

This is a classic-widgets technique. Since WordPress 5.8 (June 2021), widgets are block-based, and a Shortcode block already runs shortcodes natively, so you often don’t need this filter at all. The widget_title filter still works, but only where the legacy widget API is in play: sites running the Classic Widgets plugin, or themes and plugins that still register old-style widgets. If you’re on the block widget editor, reach for the Shortcode block first. Either way, test the result on the front end before you call it done.

Next: WooCommerce vs Shopify

Leave a Comment

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


Scroll to Top