{"id":125643,"date":"2014-02-10T11:30:00","date_gmt":"2014-02-10T16:30:00","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=125643"},"modified":"2014-11-19T18:59:26","modified_gmt":"2014-11-19T23:59:26","slug":"dont-let-your-empty-search-page-drive-off-visitors","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/dont-let-your-empty-search-page-drive-off-visitors\/","title":{"rendered":"Don&#8217;t Let Your Empty Search Page Drive Off Visitors"},"content":{"rendered":"<p>Keeping visitors on your site is vitally important, so the last thing you want for those searching your site is a page with little more than the message &#8220;no results found&#8221;.<\/p>\n<p>That&#8217;s telling visitors to &#8220;move along, there&#8217;s nothing of interest to see here&#8221;.<\/p>\n<p>But you can change this message by making your search results page work much harder. Here&#8217;s how.<br \/>\n<!--more--><\/p>\n<figure id=\"attachment_125943\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-ratio-large wp-image-125943\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/02\/default_search-700x285.png\" alt=\"Screenshot of no results search page asking visitor to leave immediately\" width=\"700\" height=\"285\" \/><figcaption class=\"wp-caption-text\">This is probably how your no results search page reads.<\/figcaption><\/figure>\n\n<p><a title=\"Read 3 Guidelines For Search Engine No Results Pages \" href=\"http:\/\/www.nngroup.com\/articles\/search-no-results-serp\/\" rel=\"noopener\" target=\"_blank\">A recent Alertbox<\/a> from UX gurus, Nielsen Norman Group, talked about search results and the consequences of having poorly designed or even empty search results pages.<\/p>\n<p>Whilst empty search results on the the major search engines are a rare occurrence, they are far more likely on the average WordPress site so are a potential huge turn-off for your visitors.<\/p>\n<p>The Alertbox made three recommendations for search results pages:<\/p>\n<ol>\n<li>Make it obvious that there no results<\/li>\n<li>Offer starting point for moving forward<\/li>\n<li>Don&#8217;t mock the user<\/li>\n<\/ol>\n<p>The first recommendation is really about the clarity of the page and making the message stand out.<\/p>\n<p>The last recommendation is targeted at those sites that attempt to use humor when no results are found. In these cases, NNG says to be careful in case the visitor thinks you are poking fun at them.<\/p>\n<p>We are going to focus on the second recommendation which NNG broke down to the following:<\/p>\n<ol>\n<li>Restate the query, so the visitor knows what they searched for<\/li>\n<li>Provide a search box pre-filled with the original query<\/li>\n<li>Suggest similar queries that do return results<\/li>\n<li>Suggest spelling corrections (as per Google&#8217;s &#8220;did you mean&#8221; functionality)<\/li>\n<li>Provide advice about modifying queries<\/li>\n<\/ol>\n<p>To meet as many of the above recommendations as possible and to try and keep visitors on our site, we are going to build a new search page to replace your theme&#8217;s current search page.<\/p>\n<h2>Overriding The Existing Template<\/h2>\n<p>Each theme will have its own approach to displaying search result pages. TwentyFourteen, which I&#8217;ll be using in this article, has a <em>search.php<\/em> template that uses the generic templates <em>content.php<\/em> and <em>content-none.php<\/em> to format the pages, depending on whether there are any results or not.<\/p>\n<p>Regardless, we&#8217;ll create our own but you&#8217;ll need to ensure that it keeps to the style your theme uses. For this reason, we&#8217;ll be doing all the modifications in the theme rather than via a plugin.<\/p>\n<p>So, first job is to add this small function to your theme&#8217;s <em>functions.php<\/em> file:<\/p>\n<div class=\"gist\" data-gist=\"8910606\" data-gist-file=\"functions.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/8910606.js?file=functions.php\">Loading gist 8910606<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This code hooks into the <em>template_redirect<\/em> action that gets called when WordPress is deciding which template to use to create the page.<\/p>\n<p>We&#8217;ve got a check in there to make sure it&#8217;s the search page that&#8217;s being requested and if it is then we are going to use our own template, <em>searchpage.php<\/em>.<\/p>\n\n<h2>Creating The New Search Template<\/h2>\n<p>In your theme directory create a new file called <em>searchpage.php<\/em>.<\/p>\n<p>As I mentioned before, you need to ensure that you maintain your site&#8217;s look and feel. If you are just playing along on TwentyFourteen, then you&#8217;ll be fine.<\/p>\n<p>If not, then one option is to copy the contents of a template such as <em>page.php<\/em> and then replace the dynamic content &#8211; generally where the Loop starts &#8211; with everything below contained in the <em>#content<\/em> div.<\/p>\n<div class=\"gist\" data-gist=\"8910606\" data-gist-file=\"searchpage.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/8910606.js?file=searchpage.php\">Loading gist 8910606<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Okay, so let&#8217;s walk through this code and see what it does.<\/p>\n<h3>Restating The Query<\/h3>\n<p>The <em>get_search_form<\/em> function from WordPress outputs the search form and automatically pre-fills the form with the search term.<\/p>\n<h3>Displaying Results<\/h3>\n<figure id=\"attachment_125945\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-125945\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/02\/search_cleaner.png\" alt=\"Screenshot of simplified search results\" width=\"700\" height=\"368\" \/><figcaption class=\"wp-caption-text\">Visitors expect search results to look something like Google.<\/figcaption><\/figure>\n<p>I&#8217;ve simplified the display of any results. NNG points out that virtually all visitors have a mental model of what a search results page looks like: it looks a lot like Google.<\/p>\n<p>By using the <em>content.php<\/em> template, TwentyFourteen\u00a0was displaying a lot of extra information, including metadata and comment counts, so I&#8217;ve stripped it down to just title and excerpt (another reason why explicit excerpts are so important).<\/p>\n<h3>When There&#8217;s No Matches<\/h3>\n<p>So, the major differences are to be seen when there are no search results to display. TwentyFourteen&#8217;s default response is to display the search form with the message that there are no search results which means that it neglects most of the NNG recommendations.<\/p>\n<h4>Adding Did You Mean? Functionality<\/h4>\n<p>We are all familiar with Google correcting our mistyped search queries or suggesting alternatives.<\/p>\n<p>Whilst we are not going to do the former, we can quite easily do the later by utilising the Wikipedia API. I chose to implement this using client-side code as I don&#8217;t want it to hold up the display of the page if the API was slow in responding or not available.<\/p>\n<p>You&#8217;ll notice that there is a placeholder<em> #didyoumean<\/em> div: this is where any suggestion message will be displayed.<\/p>\n<p>At the bottom of the template, before the <em>list_posts<\/em> helper function, there are two script tags. The second is a direct call to the Wikipedia API with the search term being inserted using the <em>get_search_query<\/em> WordPress function.<\/p>\n<p>This creates a JSON formatted payload of suggested alternatives, if there are any, that is passed to the <em>didyoumean<\/em> function courtesy of the <em>callback=didyoumean<\/em> querystring parameter on the API call. This also allows us to get around cross-domain scripting errors.<\/p>\n<p>The <em>didyoumean<\/em> function simply checks the returned data and if there is a suggestion an appropriate message is inserted into the placeholder.<\/p>\n<h4>Providing Advice About Modifying Queries<\/h4>\n<p>This is simply including content in the page. I&#8217;ve made it pretty short and sweet, so alter it to suit your needs.<\/p>\n<h4>Suggesting Similar Queries That Return Results<\/h4>\n<p>Now, this is very sophisticated functionality and overkill for the majority of sites; but providing suggestions for alternative content strikes me as being important, so I&#8217;ve gone for a more lateral solution and listed the latest five posts and the five posts with the most comments.<\/p>\n<p>This is achieved by setting up the arguments for calling <em>WP_Query<\/em> and then calling the<em> list_posts<\/em> function to run the query and output any results.<\/p>\n<p>You can change this to whatever list of posts you think is appropriate. There are plugins that will count visits and <a title=\"View this search of the WordPress Plugin Repository\" href=\"http:\/\/wordpress.org\/plugins\/search.php?q=popular+posts\" rel=\"noopener\" target=\"_blank\">provide a list of popular posts<\/a>.<\/p>\n<p>Obviously, these lists will work better the longer you&#8217;ve been collecting statistics.<\/p>\n\n<h2>The End Result<\/h2>\n<p>So, here&#8217;s the end result of our new search template.<\/p>\n<figure id=\"attachment_125944\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-125944\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/02\/search_friendlier.png\" alt=\"Screenshot of improved no results search page with suggestions and helpful tips\" width=\"700\" height=\"728\" \/><figcaption class=\"wp-caption-text\">At least we are now trying to help the visitor stay on the site a bit longer<\/figcaption><\/figure>\n<p>It&#8217;s certainly not as empty looking as its predecessor and, hopefully, with plenty of links available won&#8217;t immediately be moving your visitors on.<\/p>\n<h2>UPDATE: Automatically Performing The Suggestion Search<\/h2>\n<p>If, like Paul (see comment below), you&#8217;d like to have the search results for a suggested term automatically displayed then add this code to your <em>functions.php<\/em>:<\/p>\n<div class=\"gist\" data-gist=\"8910606\" data-gist-file=\"ajax-search.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/8910606.js?file=ajax-search.php\">Loading gist 8910606<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This adds a javascript function to the page that makes an ajax call to WordPress to execute the suggsearch function.<\/p>\n<p>You also need to make a small change to <em>searchpage.php<\/em> to replace<\/p>\n<p><code><br \/>\nif (sugg) {<br \/>\njQuery('#didyoumean').html('&lt;p&gt;Did you mean &lt;a href=\"?s=' + sugg + '\"&gt;' + sugg + '?&lt;\/a&gt;&lt;\/p&gt;');<br \/>\n}<br \/>\n<\/code><\/p>\n<p>with<\/p>\n<p><code><br \/>\nif (sugg) {<br \/>\nsuggestion_search( sugg );<br \/>\n}<br \/>\n<\/code><\/p>\n<p>The new javascript function now takes care of outputting the &#8220;Did you mean&#8221; message. It will only do so if search results are found for the suggestion as there&#8217;s not much point in suggesting an empty search.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keeping visitors on your site is vitally important but an empty search page is akin to telling visitors to &#8220;move along, there&#8217;s nothing of interest to see here&#8221;. Your search results page can work harder than that. <\/p>\n","protected":false},"author":262394,"featured_media":125943,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[186,9803],"tutorials_categories":[],"class_list":["post-125643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-search","tag-time-on-site"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/125643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/262394"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=125643"}],"version-history":[{"count":1,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/125643\/revisions"}],"predecessor-version":[{"id":193607,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/125643\/revisions\/193607"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/125943"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=125643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=125643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=125643"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=125643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}