Insert a span inside widget title to give a different color to the second word

/**
 * Wraps the first half of the provided string inside a span with the class lol_class.
 *
 * @param  string  $title  The string.
 * @return string          The modified string.
 */
function wpsa_82312_widget_title($title) {
    // Cut the title into two halves.
    $halves = explode(' ', $title, 2);

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

    // Add the remaining words if any.
    if (isset($halves[1])) {
        $title = $title . ' ' . $halves[1];
    }

    return $title;
}

// Hook our function into the WordPress system.
add_filter('widget_title', 'wpsa_82312_widget_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.

Leave a Comment