Saturday, 18 January 2014

Highlight Keywords In Search Results

Dis you know that you can highlight search keywords in your title or excerpt , if you dont then check out this simple snippet which is to be added in your functions.php file.
function wps_highlight_results($text){
     if(is_search()){
     $sr = get_query_var('s');
     $keys = explode(" ",$sr);
     $text = preg_replace('/('.implode('|', $keys) .')/iu', ''.$sr.'', $text);
     }
     return $text;
}
add_filter('the_excerpt', 'wps_highlight_results');
add_filter('the_title', 'wps_highlight_results');

No comments:

Post a comment