Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to make my widget titles two-colored; the first word white and the second one yellow. I have no idea how I can insert a span before the second word of every widget title so that I can put a color to the span.

share|improve this question

3 Answers 3

up vote 6 down vote accepted
add_filter('widget_title', my_title);

function my_title($title) {
    // Cut the title to 2 parts
    $title_parts = explode(' ', $title, 2);

    // Throw first word inside a span
    $title = '<span class="my_class">'.$title_parts[0].'</span>';

    // Add the remaining words if any
        $title .= ' '.$title_parts[1];

    return $title;

On a recent project, my client wanted graphical headers for the widgets on her WordPress blog. Unfortunately, widgets are not that easily themed. Nor can you do this in CSS.

share|improve this answer
I had to modify it a little bit, but it worked! thanks! –  Samia Ruponti Jan 20 '13 at 14:58

If it is a text widget, it supports HTML tags (like span). You can use span and other tags in the heading of that widget. Just declare the span CSS of that heading tag in your stylesheet/CSS file.

share|improve this answer
Please use proper upper-/lowercase letters. This is no chat. Thanks. –  kaiser Jan 19 '13 at 20:35
He probably wants to do it programmatically –  Nabil Kadimi Jan 19 '13 at 21:37
yes, I'm not sure if only text widgets will be used. programmatically seems to be the better solution. –  Samia Ruponti Jan 20 '13 at 14:21

for just the page title, edit page.php and replace the_title(); with:

$words = explode(' ', the_title('', '', false)); $words[0] = ''.$words[0].''; $title = implode(' ', $words); echo $title;

or for generally all titles (this will add the span around the first word in any title, such as post titles, page titles, and the_title is also used for instance in page lists) - add something like this to functions.php of your theme:

add_filter('the_title', 'span_first_word'); function span_first_word($title) { $words = explode(' ', $title); $words[0] = ''.$words[0].''; $title = implode(' ', $words); return $title; }

share|improve this answer
The OP was asking for widget titles... –  Johannes Pille Sep 27 '13 at 13:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.