Go to Top
  • No products in the cart.

Using Relevanssi “Did You Mean” for 404 Pages

Relevanssi - A Better Search

Create a better 404 page experience for your visitors, with the help of Relevanssi’s “Did you mean” functionality.

There are many resources out there for 404 page best practices (Yoast and Search Engine Watch) to name a couple. The basic idea is that you don’t want to leave your visitors at a dead end. You want to give them helpful ways out.

We’re big fans of the Genesis Framework by Studiopress [note: that’s an affiliate link] – 90% of all of our WP builds over the last 2-3 years have been done with Genesis. Their basic 404 page offers a good start by adding the search bar.

For Sallie Bingham’s website relaunch, we wanted to go a bit further—we wanted to only offer a link to the homepage but to take a stab at guessing what the visitor was after (if for example they tried to key in the URL manually from memory, and had a small typo), or give them something else related they might be interested in.

We’re also big fans of Relevanssi, Mikko Saari’s very established WP search augmentation plugin. The Premium version is one of the few plugins we always use on a site, without question.

Luckily, even the free version of Relevanssi includes a “Did you mean?” function built in, which is generally meant for the search results page in the case of a typo (more details) – but can be used in other places, like on a 404 page.

In the case of Genesis, and most themes, this is most easily accomplished by copying the 404.php page from your theme’s main folder into your child theme, and then adding the code below.

For this Relevanssi “Did you mean” 404 Page addition, all we’re going to do is parse the current URL for the page/post title (the end of the URL), pass that string to Relevanssi, and let it do it’s magic. If Relevanssi doesn’t find any similar results, the “Did you mean” will automatically not be shown which is perfect for our needs. And, just so you know, it won’t always find hits—or in the case of huge amounts of posts, might find too many – the ‘5’ below will hide Relevanssi if more than 5 posts are returned (this is the default).

Note: we’re deliberately not removing the hyphen, in our testing it wasn’t necessary. Without examining Relevanssi’s code, our guess is Mikko is processing the query on that end.

Hit us up in the comments below if you have any questions!


function getLastPathSegment($url) {
    	$path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
    	$pathTrimmed = trim($path, '/'); // normalize with no leading or trailing slash
    	$pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash

    	return end($pathTokens); // get the last segment
}

$lastSegment = getLastPathSegment($_SERVER['REQUEST_URI']);
$escSuggestedTerm = esc_attr( $lastSegment );

if (function_exists('relevanssi_didyoumean')) {
	relevanssi_didyoumean($lastSegment, "<p>But maybe you meant: ", "?</p>", 5);
	} else {
	     // do nothing
	}

, , ,

4 Responses to "Using Relevanssi “Did You Mean” for 404 Pages"

  • Mikko
    March 6th, 2015 - 11:13 pm Reply

    Hi, this is indeed a very clever idea and should work quite well – with Premium. The free version may or may not work, the Did you mean feature is less reliable there and depends on what kind of search logs you have as a source material.

    You don’t have to worry about hyphens, Relevanssi cleans out hyphens and other punctuation from the search queries.

    • john
      March 7th, 2015 - 9:38 am Reply

      Thank you, Mikko — do appreciate you taking the time to reply and clarify these things. Premium is very worthwhile for many different reasons, and this is definitely another one then.

  • Henrik
    August 6th, 2015 - 7:12 am Reply

    Can you post an url to a site where this has been implemented?

Leave a Reply

Your email address will not be published. Required fields are marked *