Highlight Keywords in Search Results within the_excerpt and the_title

Snippets by IsItWP

Highlighting the keywords in search results within the post title and excerpt will let your users know what they’ll find in the article.

Instructions: Add to the following code to the functions.php of your WordPress theme.

function wps_highlight_results($text){
     $sr = get_query_var('s');
     $keys = explode(" ",$sr);
     $text = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">'.$sr.'</strong>', $text);
     return $text;
add_filter('the_excerpt', 'wps_highlight_results');
add_filter('the_title', 'wps_highlight_results');

You may also enjoy replacing keywords in the_content and the_excerpt.

Comments   Leave a Reply

  1. hi, hope this finds you weel, thanks for the coding!

    i’m a portuguese speaker and i need the search to match the accents as weel(:

    for ex: if i search ‘mae’, it doesn’t hightlight the result, as in the text it shows ‘mãe’.

    how can i surpass this accent issua,please?

    kind regards

  2. Found issue with this function, if you search for a term that is exactly the same as a navigation menu item it messes the navigation, duplicates the key words inside the menu item along with couple of other issues.

    Any way of getting around this?
    Thank you

    1. It may be conflicting with the theme. You may have to edit how the style is applied.

  3. it works like a charm. thank you

    1. Good to know!

  4. johannes khampepe December 29, 2016 at 1:07 pm

    it works a magic…. muito obrigado.

  5. it messes up links that contain the search terms

  6. i have a function that limit my excerpt , how can i do that with it ?

  7. Thank you very much for this snippet. I have found a small issue with it though — it de-capitalizes the search term in the search results.

    For example, if I have a post titled “Include password protected posts” and then search for “include”, the title of my post in the search results will be displayed as “include password protected posts”.

    Any idea how this could be fixed?

    1. Harry William Love April 2, 2013 at 3:24 pm

      Change the replacement string from




      1. Peter Onyegbule July 7, 2020 at 9:50 am


        I’m not fully deep into PHP coding so when I replaced the strings, I got errors.

        My site almost went down.

        1. You may want to check out this page: https://www.isitwp.com/properly-add-code-snippets-wordpress-site/

          Did you see any error messages? If so, what were the messages?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!