Enlight searched text in wordpress search results

January 10, 2009 · Print This Article

Many WordPress users find the search function a bit imcomplete. For exemple, it don’t automatically hightlight the searched text.

Open your search.php file and find the the_title() function. Replace it with the following:

echo $title;

Now, just before the modified line, add this code:


<?php
$title = get_the_title();
$keys= explode(" ",$s);
$title = preg_replace('/('.implode('|', $keys) .')/iu',
'<strong class="search-excerpt">\0</strong>',
$title);
?>

Save the search.php file and open style.css. Append the following line to it:

strong.search-excerpt { background: yellow; }

That’s all.


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.