How to Insert Line Break in Widget Title

WordPress, by default, does not allow HTML tags in widget title.  If you try to add HTML tags in the widget title, they will be stripped before the widget content is output. So if you want to add Line Break in the widget title, you will need to perform some code jugglery.

In this post I will share a simple technique which you can use to insert line break in the widget title. The concept is simple.

We will create a filter which hooks into Widget_title filter and dynamically add the break tag.

Here are the details.

Create the filter

function custom_widget_title( $title ) {
    $title = str_replace( 'spice_line_break', '<br/>', $title );
    return $title;
}    
add_filter( 'widget_title', 'custom_widget_title' );

The custom_widget_title function hooks in widget_filter and searches for an identifier.

If the identifier is found, the code simply replaces it with the break tag. Here I am using spice_line_break as the identifier. You may use anything as an identifier.

Seeing it in action

Use the code snippet plugin to activate the snippet.

Now use the identifier text in the widget title. Here is a screenshot to make things simple.  This is how the title will appear now. As you can see the line break is applied where the identifier text is used.

Did you enjoy this article?
Get Awesome with WordPress. Subscribe and get awesome stuff delivered straight to your inbox.
I agree to have my personal information transfered to Mad Mimi ( more information )

Leave a Comment