WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I get the search terms highlighted without plugin?

share|improve this question

Add these 2 functions to your functions.php

function search_excerpt_highlight() {
    $excerpt = get_the_excerpt();
    $keys = implode('|', explode(' ', get_search_query()));
    $excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt);

    echo '<p>' . $excerpt . '</p>';

function search_title_highlight() {
    $title = get_the_title();
    $keys = implode('|', explode(' ', get_search_query()));
    $title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);

    echo $title;


To use the_content for your search results use the function below:

function search_content_highlight() {
        $content = get_the_content();
        $keys = implode('|', explode(' ', get_search_query()));
        $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content);

        echo '<p>' . $content . '</p>';

In your loop or search.php file call <?php search_title_highlight(); ?> instead of <?php the_title(); ?> and use <?php search_excerpt_highlight(); ?> instead of <?php the_excerpt(); ?>

In your css add the search-highlight class which will highlight all searched words in yellow.

.search-highlight {
share|improve this answer
Apply preg_quote() to $keys to prevent your regex from blowing up in case of special characters like parentheses or brackets. – Geert May 1 '11 at 7:09
What about highlighting the search term after the user clicks on the single and goes inside the post? Then the get_search_query() returns an empty string – Maor Barazany May 22 '11 at 23:47
Those should be filters for the_excerpt and the_content instead. Anyway: Nice answer, but the comment from @Geert could be worked in :) – kaiser Oct 13 '12 at 0:25
It apply <strong class="search-highlight"> code in our readmore link also if it have search term, How can we solve that. – user22614 Oct 18 '12 at 7:20
it is replacing the text in the readmore href also? how to fix this? – Naveen Jan 13 '14 at 12:58

The above works well I've run the similar code, but tie the title and excerpt together. But found it breaks when someone enters a space " " either at the beginning or end of a search query term.

So Ive add this line:

$keys = array_filter($keys);

// 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');

Hope this proves to help others.

share|improve this answer

The above solutions break the page if the search term appears inside HTML tags. You should use something like:

      $regEx = '\'(?!((<.*?)|(<a.*?)))(\b'. implode('|', $keys) . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'iu';
      $text = preg_replace($regEx, '<strong class="search-highlight">\0</strong>', $text);
share|improve this answer
thanxs mate you made my day :-) – Agha Umair Ahmed Jan 26 at 11:56

protected by toscho Oct 29 '12 at 11:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.