WordPress add_filter() for Widget Titles

Today I decided to post this little snippet of code that is being used in my latest 2012 WordPress Theme Splendid.

During the design phase I wanted to use beautiful widget titles with a custom font.

I wanted to have the first word wrapped inside a <span></span> tag so that I can apply a bold effect (or in this case, a custom bold font).
But herein lies the problem: WordPress does not allow any type of tags in your widget titles.

This is the function that I ended up writing, it might not be the best way to do it, but it gets the job done:

/*  Widget Titles
/*  Author: Dumitru Brinzan (https://www.brinzan.com)
/*  Params: None

add_filter ('widget_title', 'wpzoom_fix_widgets');
function wpzoom_fix_widgets($old_title) { 
    $title = explode(" ", $old_title,2);
    $titleNew = "$title[0] $title[1]";
    return $titleNew; 

What it does is wrap the first word inside the required <span></span> tag and leaves all the rest as it is.
Drop it inside the functions.php file of your theme, you don’t have to do anything else with it.

Mission accomplished.

If you know of a better way to achieve the same result, leave a comment below.

Next Article — Newer

2 Replies to “WordPress add_filter() for Widget Titles”

  1. Amanda

    Had to change it slightly to:

    $titleNew = "$title[0] $title[1]";

    (I’m guessing that’s what yours actually looks like & it just got stripped or something.)

    Thanks for pointing me in the right direction!

Leave a Reply

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