Md Mehedi Hasan

Search Result Highlight WordPress

functions.php

function alpha_highlight_serach_result($text){
	if(is_search()){
		$pattern = '/('.join('|', explode(' ', get_search_query())).')/i';
		$text = preg_replace($pattern,'<span class="seach-highlight">\0</span>', $text);
	}
	return $text;
}
add_filter('the_content', 'alpha_highlight_serach_result');
add_filter('the_excerpt', 'alpha_highlight_serach_result');
add_filter('the_title', 'alpha_highlight_serach_result');

style.css

span.seach-highlight {
    background-color: #ff0;
}