Sometimes the seemingly easy things aren’t that easy after all. Someone would think that adding simple html code to a widget title (for example to create a link out of it or to break it in two lines) would work out of the box, but nope, wordpress goes and strips away all of your nice html code that you place within your widget title…

Workaround? Luckily it exists. Thanks to thewebprincess.com (such a beautiful address btw) I found this helpful code that you can also place within your functions.php file of your child theme

add_filter( ‘widget_title’, ‘your_html_widget_title’ );
/**
* html_widget_title function.
*
* @access public
* @param mixed $title
* @return void
*/
function your_html_widget_title( $title ) { //HTML tag opening/closing brackets
$title = str_replace( ‘[‘, ‘<‘, $title );
$title = str_replace( ‘[/’, ‘</’, $title );
$title = str_replace( ‘]’, ‘>’, $title );
return $title;
}

Now go and insert your beautiful html code in the title such as a nice <br/>  to brake the title in two lines. Doesn’t work? Heh, didn’t do the trick for me either. BUUUT don’t go away.

I looked at the above code more closely and I saw that it replaces the with <. So, I thought with whatever little remained from my brain after a long day’s work to write my code like that. [br/] instead of <br/>. 

Same trick if you want to wrap your title in <a> tags. Use [a href=….]Your title [/a]. And it works.

Leave a comment

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