Custom title widget / HTML encoding

As you know, html tags doesn’t work on widget title. But there’s work around to use it. The best approach i know is to use shortcode in title. So, for using br and span, the following is a solution –

add_filter('widget_title', 'do_shortcode');

add_shortcode('br', 'wpse_shortcode_br');
function wpse_shortcode_br( $attr ){ return '<br />'; }

add_shortcode('span', 'wpse_shortcode_span');
function wpse_shortcode_span( $attr, $content ){ return '<span>'. $content . '</span>'; }

add_shortcode('anchor', 'wpse_shortcode_anchor');
function wpse_shortcode_anchor( $attr, $content ){ 
    return '<a href="'. ( isset($attr['url']) ? $attr['url'] : '' ) .'">'. $content . '</a>'; 
}

You can simply put these lines to your functions.php file.

Usage:

As like shortcode usage on post content, use it on the widget title.
[br]
[span]something[/span]
[anchor url="http://domain.com/"]something[/anchor]

One issue

Most of the widgets use a filter on widget title widget_title, but some custom widget might not apply or use this hook. So, on those widgets, this method will not work, and i guess other method wouldn’t.

Leave a Comment