How to Highlight Search Terms in WordPress

How to Highlight Search Terms in WordPress

If you are using a search box on your WordPress blog, you can show the related search terms. This helps the readers to know if they are getting the info or not.

For keeping the user attraction, you need to highlight the related search terms. There are two different ways to highlight the related search terms. One is using the plugin, and the other one is using code.

You can use any of them according to your preference. In this post, I am going to tell you details about highlighting related search terms.

highlight search terms

Showing Related Search Terms Using Code

For showing the related search term with code, you need to open the search.php file in your theme file folder inside your wordpress admin dashboard. Now, look for the following code:

1

<?php the_title(); ?>

 

Now make sure that you paste the following lines above the title code:

1

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

 

Now, open the CSS file and add the styling for your class search excerpts. You can then add the following code:

1

2

3

4

strong.search-excerpt {

background-color:yellow;

color:blue;

}

Source: Michael Martin.

However, you can edit the color according to your site’s color scheme.

Complete the above process and done.

Highlight the Search Term Using Plugin

If you want to avoid the annoyance of using codes, simply use a plugin for the task. There are different plugins for highlighting related search terms. Here, I am going to share two plugins for it.

1. Using the Relevanssi – A Better Search Plugin

There is both free and premium version of this plugin. You can start with the free version. It provides the opportunity to modify the search result. There are a variety of options and configuration available in it.

It will make the search result presentation better than ever. You can modify the search result according to relevancy. It will find documents matching either just one search term or all words. You can also set this plugin for the related search phrase. It will highlight the search term in the search result.

Go to the WordPress admin dashboard, find the “Plugins” option and click on it. Then go “add new” plugin, you will see there a search option, type this plugin name in search box and enter. Finally you will see the plugin in search results. Download this plugin and activate it.

You can use this plugin for relevant search terms, sort out by date, etc. However, there are so many customizations available in it. You can visit the plugin page for additional detail.

2. Using Search Everything Plugin

This is another popular plugin to highlight the related search terms. You can configure this plugin for search pages, attachments, drafts, excerpts, comments, tags, etc.

This is a lightweight plugin that you can easily install on your newly started blog. It will highlight the search terms according to your given preferences.

For installing this plugin, first, go here. Now, download the plugin which will be a .ZIP file. Now, unzip the file and put them into ‘search-everything’ directory into your wp-content/plugins/ directory.

Go to the plugins page in your WordPress admin panel and click activate. Go to Settings > Search Everything and then configure it.

You can also download this plugin with same method recently I explained for Relevanssi – A Better Search Plugin. You will find a variety of options. From them, you can choose only the necessary features.

Which One is Better, Coding or Plugin?

Well, if you know coding, you should go for coding. You can customize the whole thing as you wish. You can change everything whenever you want. But you need to do it from scratch.

But, when you are using a plugin, you need to stick with the available options.

However, there are a variety of options in the above plugins so that they may fulfill all your requirement. So, the decision is yours. Both are good for highlighting related search terms.

So the steps to highlight the search terms are-

  1. Find this code <?php the_title(); ?> and replace with <?php echo $title; ?>.
  2. Install and activate the A Better Search Plugin. Or,
  3. Install and activate the Using Search Everything Plugin.
Final Words

Finally, I have showed all the methods to highlight the search terms in serach results on your wordpress website.

It will make easy to get whatever you are searching on site. I hope you liked this post, share this post with the needy one. You can read also here: How to highlight author’s comments in wordpress.

 

 

 

Write a Comment