Want to highlight search terms without plugin in WordPress? Use following code in your theme’s functions.php file OR in site specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 | // Add Bold to searched term function highlight_results($text){ if(is_search() && !is_admin()){ $sr = get_query_var('s'); $keys = explode(" ",$sr); $keys = array_filter($keys); $text = preg_replace('/('.implode('|', $keys) .')/iu', ''.$sr.'', $text); } return $text; } add_filter('the_excerpt', 'highlight_results'); add_filter('the_title', 'highlight_results'); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.