How to Highlight Search Results in WordPress
Today i am showing How to Highlight Search Results in WordPress Website. Search results in WordPress are a very important part of the website. The user can easily find useful content on the website, if they know where to look for it.
Search results are a great way to show your visitors what they want. However, it’s not always possible to show them everything that they are looking for. This is especially true if you have a large number of keywords in your site.
Search results are a great way to show your visitors what they want. However, it’s not always possible to show them everything that they are looking for. This is especially true if you have a large number of keywords in your site.
Paste this code on your function.php file
// highlight search result
function skyboot_search_result_highlight($text){
if( is_search() ){
$pattarn = '/( '.join( '|', explode(' ', get_search_query( )) ).' )/i';
$text = preg_replace($pattarn, '<span class="search-excerpt">\0</span>', $text);
}
return $text;
}
add_filter( 'the_content', 'skyboot_search_result_highlight' );
add_filter( 'the_excerpt', 'skyboot_search_result_highlight' );
add_filter( 'the_titLE', 'skyboot_search_result_highlight' );
Paste this code on your css file
span.search-excerpt {
background-color:yellow;
color:red;
}