Bing spelling suggestions and WordPress search

I’m a truly terrible speller, so when I was testing out the search functionality on this site it became very clear to me how much I rely on Google’s spelling suggestions and how deficient any search without that feature feels. I was surprised to find that there were no existing WordPress plugins that could easily add this functionality, so I decided to see how hard it would be to implement that functionality myself. As it turns out, with the right API account a simple spelling suggest feature is fairly straightforward to build.

Getting the suggestions

The first hurdle in this project is where to get the spelling suggestions. A little bit of searching revealed that my best bet was probably to use the search APIs from one of the big boys in the search game. Google’s API unfortunately doesn’t include spelling suggestions, and Yahoo’s isn’t free (as much as I wanted this, I wasn’t willing to pay for it). That left Bing, and luckily they provide spelling suggestions in their web service and have an intro level of 5000 free queries per month (way more than I’ll ever need).

Getting started with the Bing API was a little frustrating at first because I immediately found this guide to working with the API, and it took me an embarrassingly long time to figure out that it was out of date and no longer correct. Once I got through that and found a proper guide, things got substantially smoother.

The PHP examples provided in the documentation are fairly thorough, and I followed them almost exactly. I was able to get the search query using WordPress’s built in get_search_query() function, although I did experience some issues with special character escaping misbehaving between WordPress and Bing. Eventually I discovered that calling get_search_query(FALSE) instead and then passing the resulting string through urlencode() solved the issue. I also did not wrap the query in single quotes before escaping it as recommended by the guide, as I was unable to get the API to function when used that way.

Using the suggestions

Once the suggestion has been gathered it needs to be presented to the site’s visitors in a usable way. While I can see some value to returning spelling suggestions with every search, because of the limited per-month nature of the API I decided to only query Bing for a spelling suggestion on searches that return no results. I accomplished this by placing the PHP above in the section of “search.php” that gets called when a search returns no results. This section of the file should be pretty easy to find, just look for your theme’s “Sorry, no results” text.

I wanted to display the suggestion text only if Bing actually responded with a suggestion, so I did a quick if... else block to check if the suggestion variable had a value, and fall back on generic “no results” text if it didn’t. I also inserted the suggestion as a link by running the suggestion through urlencode() and adding it to the query string to get a URL for a results page that used the suggested spelling.

The result is a very clean and professional feature that considerably streamlines site search and does so without too much difficulty. To get a better look at the end result, try using the search box at the top of this page to search for “javascrpt” or “sharepont”.

Fun Fact: Even though this post includes the two misspellings above, it will not show in the results list if those misspellings are used as search terms. This is achieved by inserting one of the unused ISO-8859-1 entities (in this case ) in the middle of each word. This makes no visual difference when viewing the page, but changes the word in the search index.

Tagged with: , , ,
Posted in PHP, Server Side Code, WordPress
5 comments on “Bing spelling suggestions and WordPress search
  1. Skip says:

    Hi Ben,

    I’ve been looking for something like this. Looks brilliant – most of the plugins are either badly out of doubt or overkill for what I need. I understand where the second block of code would go in my templates but am unclear on the placement of the first block once I’m signed up in Bing? Can you advise?

    I’m not a coder but have been playing with search templates, etc mainly for aesthetics rather than functionality although my results and no results pages are now much better I think.

    • Skip says:

      I’m so sorry – I should have read more carefully, I think you’ve explained where it goes. I guess the 2 blocks are basically combined.

      • Ben says:

        Hi Skip, thanks for reading and I’m glad this has been of some assistance to you.

        You’re basically correct. They should both go in the search results PHP file in your theme (usually search.php), the first chunk connects to the Bing service and gets any spelling suggestions, the second section inserts the top suggestion into the page.

        As far as exactly where within your search.php file this will need to go, that’s up to you to figure out as it will vary from theme to theme. Mine has a section of search.php specifically for what happens when search returns no results, so I put this code in that section.

        Other than that, just make sure to sub “YOUR_BING_SEARCH_ACCOUNT_KEY” for your actual account key, and everything should work from there.

  2. Skip says:

    Hi Ben, I have a couple of follow up questions. First up this does exactly what I wanted but as always you want a bit more don’t you?

    1. Can this code be adapted easily to display more than 1 suggestion. I was comparing to what Google did and for the same search it shows 3. I know that sounds greedy but just wondered whether there’s a simple way to do that?

    2. Having suggested a search is it possible to show say 2 results from the suggested search if they exist?

    I’m not asking you to spend hours coding but if there’s something quick and simple to do that it would be great. If not no problem at all. It’s brilliant as is.

Leave a Reply

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

*