Search Results Highlighting Without Using Plugins

There are quite a few plugins for improving search in WordPress. Some of them change the search algorithm, while others make the search more convenient. This article will focus on the latter type of plugin. I want to slightly enhance the standard WordPress search: to highlight the searched words.

Task: Highlight search results without using plugins. This is accomplished by the method presented below, specifically with a function that is essentially analogous to perhaps the simplest plugin for highlighting search results in WordPress — Search Hilite (words are highlighted with no additional actions).

Place the code in the functions.php template file.

add_filter( 'the_content', 'kama_search_highlight' );
add_filter( 'get_the_excerpt', 'kama_search_highlight' );
add_filter( 'the_title', 'kama_search_highlight' );

/**
 * Highlight search words in specified text.
 *
 * @param string $text The text you want to highlight the words in.
 *
 * @return string
 *
 * @version 0.1
 */
function kama_search_highlight( $text ){

	// settings
	$styles = ['',
		'color: #000; background: #99ff66;',
		'color: #000; background: #ffcc66;',
		'color: #000; background: #99ccff;',
		'color: #000; background: #ff9999;',
		'color: #000; background: #FF7EFF;',
	];

	// for the search pages and the main loop only.
	if( ! is_search() || ! in_the_loop() )
		return $text;

	$query_terms = get_query_var( 'search_terms' );

	if( empty( $query_terms ) )
		$query_terms = array_filter( (array) get_search_query() );

	if( empty( $query_terms ) )
		return $text;

	$n = 0;
	foreach( $query_terms as $term ){
		$n++;

		$term = preg_quote( $term, '/' );
		$text = preg_replace_callback( "/$term/iu", static function( $match ) use ( $styles, $n ){
			return '<span style="'. $styles[ $n ] .'">'. $match[0] .'</span>';
		}, $text );
	}

	return $text;
}

The function will work if the search.php file uses the the_excerpt() or the_content() function to display search results. If the output is done differently, refer to the following.

At the beginning of the code, you can adjust the styles of the found words: $st1 - first word, $st2 - second, and so on.

By default, the colors are as follows: Highlighting search results, without plugins.

It's better to add the code directly to the search file, just before the content, because if you add it to functions.php, due to the hook:

add_filter( 'the_title', 'kama_search_highlight' );

It will also highlight words in the site menu, which is outputted through wp_nav_menu().

Highlighting Anything

If functions other than the_excerpt() or the_content() are used for displaying the search, the output text needs to be passed through this function for the highlighting to work.

Suppose you use the get_my_excerpt() function for displaying search results, then for the highlighting to work, the output should be recorded as follows:

$search_result = get_my_excerpt();

echo kama_search_highlight( $search_result );

Updates

29.04.2010
Thanks to Y.B. for the suggestion (in the comments) to use the u modifier for the regular expression - the code was significantly reduced.

21.04.2010

  1. There were some unclear bugs with the highlighting - not all words were highlighted. As it turned out, the problem was in the regular expression, I simplified it, and now everything is highlighted.

  2. The highlighting is now case-insensitive for Cyrillic, i.e. if the search specifies, for example, the word "поиск", the words "Поиск", "поиск" or "ПОИСК" will be highlighted. This problem also exists in the Search Hilite plugin. I've been wanting to fix this bug for a long time - had to sweat until I figured out where the problem was.