Using Multiple Titles In a WordPress Widget

Today I needed a way to add more than one line of text for a widget heading, each line of text also needed to be styled in a different way. Here is what I came up with as a solution.

Basically inserting three semicolons in a row inside a widgets title splits the content either side of the semicolons into their own span elements with their own class name.

function multiple_widget_title($title) {
	// Set a string of characters to use as the break point. 
	$breakat = ":::";
	// check if the break point exists in the title	
	if (strrpos($title,  $breakat) === false) { 
		return  $title;		
	} else {
		$counter = 1;
		$output = '';
		$split = explode($breakat,$title);
		foreach ($split as $chunk) {
			$output .= '<span class="widget-title-' . $counter . '">' . $chunk . '</span>';
			$counter++;
		}
		return $output;
	}
}
add_filter( 'widget_title', 'multiple_widget_title' );

One Response to Using Multiple Titles In a WordPress Widget

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">