Using wordpress native search, you can use this trick to highlight the search term in the title in the search results. This can be done by tokeinizing the title and then assigning a css class to the search word. This css class can be styled with background color to highlight the search word. Use this code in funtions.php

if(!empty($getvars[s])){
			$title = get_the_title();
			$keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title);
                        }
//To display the title use
echo $title;
//instead of
  the_title();

Last but not the least, paste this in your style.css

.search-excerpt{
background-color:#ffff00;
}

You can also use this plugin to achieve the same funtionality without writing any code yourself. Hope you like this wordpress snippet 🙂

Yumna Tatheer

I am a Computer Science graduate and a part time blogger from Lahore, Pakistan. I am a PHP programmer and a wordpress expert.

More Posts

Follow Me:
Facebook