Advertisement
  1. Code
  2. Plugins
Code

Enhancing the Search Form With typeahead.js

by
Difficulty:IntermediateLength:ShortLanguages:

The WordPress search form doesn't offer any bells and whistles by default. If it's already included in your theme, or you've added the search widget to one of your sidebars, you can attest to that. One way to greatly improve its functionality is to include an autocomplete script to help enhance the efficiency of how relevant search queries are relayed.


Twitter typeahead.js

There are quite a few autocomplete scripts available and recently Jake Harding from Twitter released typeahead.js, a completely independent version of a similarly named script that comes packaged with Bootstrap. I thought it would be interesting to figure out how to use this lightweight script with WordPress, and after a bit of fumbling around, I managed to put together a little plugin with the help of Michal Bluma.


WP Typeahead Plugin

The plugin is pretty straightforward since there's only a little bit of customization that is needed to get things working properly. I'll break down each section of the plugin code for you to explain what's going on.

Basic Plugin Setup

On its own, the above code will only spit out some errors but this is the start of everything that you need to use typeahead.js with the WordPress search form. The first action is there to enqueue the JavaScript / CSS files you need and place the required JS in the footer. Next come the Ajax calls to help out with the search results. Let's take a look at each function.

Enqueueing

There are four files that need to be enqueued. First is the actual typeahead.js file, then the templating engine called hogan.js, followed by a new JavaScript file we'll create to put everything in motion, and finally the stylesheet that makes it all look good.

You'll notice in the middle there we also use wp_localize_script to make the Ajax URL available for our JavaScript.

The Footer Script

Put this into the new /js/wp-typeahead.js file. The jQuery selector above will attach the typeahead function to any of the default WordPress search forms and use the Hogan templating engine to format the returned Ajax data. Sometimes, the search form can be modified by your theme and the submit button will be removed so I've added in a little script to make sure that when you hit the enter button, the search form is actually submitted.

The Ajax Query

When something is typed into the search form, typeahead.js will take it and submit it through Ajax using the remote parameter from the code in the last step. That text has to pass through a function in order for it to be useful and that's why you need the little snippet above.

It takes the search text, runs it through a WordPress query to return any relavent results if they exist. Those results are sent back after being encoded using JSON so that the script can read the data properly.


The Completed Code

With everything in place, the main plugin file should look like this...

When you download the plugin, you'll have the stylesheet and required JS files included within the ZIP file.


Conclusion

Adding an autocomplete script to your search form can help your users navigate through your site more easily. That means a better overall experience which will hopefully lead to more repeat visits and higher traffic. This plugin just needs to be activated in order for it to work with your search forms.

If you have any comments or feedback on anything you read above, please feel free to discuss it below.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.