Relevanssi, the Best Search Plugin for WordPress

Relevanssi, the Best Search Plugin for WordPress

Share on facebook
Share on twitter
Share on pinterest

It’s not the first time I installed Relevanssi on a WordPress site. Initially, I just wanted a tool that can search in the content of custom post types that I create with WCK, for things like product FAQ or documentation purposes. Normal WordPress search couldn’t do its job well enough. It’s outright unconfigurable. Therefore, I needed something that integrates automatically, has a few advanced options but doesn’t go over the top (more on that later). Also, I imagined that a plugin could do this for free.

Finnish developer Mikko Saari’s Relevanssi is quite popular, and after trying a few alternatives, I happily chose this. I’m glad to spread the word about it, in hopes that your WordPress site will become better.

What can Relevanssi do for you?

It’s more of a behind the scenes plugin, the joys of which are only felt upon performing an actual search. It won’t touch the search box. It just changes the results, not the looks. That’s why it’s so easy to integrate and just to try out.

  • Most importantly, it shows more relevant content. Simple as that.
  • It builds an index of your content, based on what you need. I tick all the (custom) post types I want it to search in, and leave obscure ones unticked. There are options to exclude by page/post ID or even whole categories.
  • Relevanssi not only highlights your search term in the results, but it also creates an excerpt with the context where it found your words. This feature helps you better judge the relevance.
  • It’ll match partial words too if there are no hits for whole words.
  • It can handle synonyms and skip stopwords.
  • For more, check out the features on their site.

How Relevanssi search results resemble Google

While you can tailor the looks however you want (not Relevanssi’s job), if the results are irrelevant, visitors will end up frustrated. Here, Relevanssi shines with two unbeatable aspects.

First, its algorithm. It has a ranking system that orders the results based on relevance. Exact matches get a boost. You can even influence the weighting factors that score results based on your term appearing in the content, titles, comment text, tags, and categories. I’ve always got highly relevant results, and it does find everything I expect. For example, try searching here for “updraftplus” (another plugin I praise). It’ll rank results starting from its actual review to those where I merely mention it. Somehow I don’t expect native WordPress search to be this smart, so I will never go back.

Second, the highlight feature. You might remember that Google used to have this yellow marker for your search term when you opened the Cached view. They don’t do that anymore, but I always liked it before browsers added a quick search feature that comes with “highlight all occurrences”. However, in the context of site search, I tend to view this feature as a nice UX touch. Whenever you go to an article from our search results page, your original search term will appear highlighted.

Our special highlight with mark.js (optional!)

I like the search query highlight feature a lot, but it caused a problem for us. We show code snippets on this site, but the function that Relevanssi hooks into the_content runs a html_entity_decode() on the output. That messed things up, so we opted out from its PHP-based processing and moved on to a custom JS solution. It might have implications for caching plugins too.

/* Prevent Relevanssi PHP-based search keyword highlight */
function lwp_4572_init() {
    remove_filter('the_content', 'relevanssi_highlight_in_docs', 11);
}
add_action('init', 'lwp_4572_init', 11);

The reason we don’t disable it in the settings, is that we still want the term to be in the URL (?highlight=updraftplus). Instead of highlighting with PHP, the following snippet will do the job with JS. It’s is dependent on mark.js so make sure to include that on your site!

;(function($) {
    $.urlParam = function(name, url) {
        if (!url) {
            url = window.location.href;
        }
        var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
        if (!results) {
            return undefined;
        }
        return results[1] || undefined;
    }
    $(document).ready(function() {
        var hl = $.urlParam("highlight");
        if (hl) {
            $("p").mark(hl, {
                "className": "relevanssi-query-term",
                "diacritics": false
            });
        }
    });
})(jQuery);

In my article that helps you add custom scripts to your WordPress site, I only mention PHP snippets. You can easily load the above JS if you save it as custom.js in your child theme and load it like this:

function custom_scripts() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri().'/custom.js',
        array('jquery'),
        false,
        true
    );
}
add_action('wp_enqueue_scripts', 'custom_scripts');

You either want to load mark.js separately or also add it to custom.js (like we did). The WordPress functions used are add_action() and wp_enqueue_script(), just in case you wish to learn more.

Why not go over the top?

I decided to settle with Relevanssi free, as it’s just good enough. It has a pro version with extra features, but I never needed more. Not sure why I feel the need to express it, but these reviews aren’t paid, and I’m not writing them to stuff affiliate links everywhere.

What I meant by going over the top, is that you could copy what the big guys do. Check out Algolia with a site almost as lovely as Stripe. It might be costly unless you are non-commercial, but it could be a perfect solution. That has off-site indexing as it all happens in the cloud, boosted with autocomplete and everything you could ask for. For the rest of us mortals, Relevanssi is still the best (and free) WordPress search replacement.

This site is powered by Elementor

  • This site is powered by Elementor

Related Posts

Check out Justified Image Grid, my top-selling WordPress gallery that shows photos without cropping!

Show your photos with Justified Image Grid!