Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Create a Live Search in WordPress with jQuery and Ajax

Posted on May 25, 2012 by Pippin in Advanced, Ajax, Member Restricted, Subscriber Only, Tutorials, WordPress Admin / Dashboard, Working with jQuery, Working with Users 15 Comments
Home» Tutorials » Advanced » Create a Live Search in WordPress with jQuery and Ajax
Tweet
Love It - 5

A live search is a search form that displays the results for your search query as you type. When done right, they can be exceptionally nice to use, and going through the process of building one covers a lot of really important techniques that can be applied to many other areas as well. This in-depth video tutorial will walk you through the process of creating a live search field within the WordPress dashboard.

Tutorial Teaser


The video is pretty lengthy, but I hope you can stick with me through it all because there are some really great techniques demonstrated. In the video, I create a live search form for the WordPress user database, and integrate it into my Restrict Content Pro plugin. Using my plugin as an example works really well because it gives some context to what is being built, though the code itself can be applied to anything.

Please note that this is an advanced tutorial. If you are not familiar with how to load scripts in WordPress correctly, or how to use ajax within WordPress, please read / follow the tutorials below:

  • Loading Scripts Correctly in the WordPress Admin
  • Process Ajax Requests Correctly in WordPress Plugins
Tweet Follow @pippinsplugins
add_action, Ajax, jquery, search, wp_ajax

15 comments on “Create a Live Search in WordPress with jQuery and Ajax”

  1. stomp says:
    May 25, 2012 at 1:42 pm

    Excellent tutorial, was actually good you stumbled across problems as those are the problems other people have too.

    I was wondering – if the username doesn’t relate to the person’s name and therefore you wanted to search by display name (most likely to be their first name and last name) how would you get their ID following the search?

    So just to elaborate. Your username is wpguru, however I want to be able to find your name by searching for ‘Pippin’. Is it possible to do this?

    Cheers!

    Reply
    • Pippin says:
      May 25, 2012 at 2:09 pm

      Thanks! I think demonstrating the actual errors, and solving them live, really helps people learn.

      It would be possible to search for users by the first, last, or display name, but you would have to write a custom function and SQL query. The get_users() function does not support searching any of those fields. It only searches ID, email, and username.

  2. philderksen says:
    May 27, 2012 at 11:50 am

    Thanks for the in-depth tutorial Pippin. It’s definitely good to see real world debugging even if it’s for a variable name typo here and there.

    If I’m not mistaken the entire jQuery UI library is included in WP core so you could use their autocomplete widget for a different style.

    http://jqueryui.com/demos/autocomplete/

    I definitely want to experiment with different live search scenarios both on the admin and public sides.

    Reply
    • Pippin says:
      May 28, 2012 at 9:56 am

      Yes, the UI library is included in core. Using that would definitely be a good idea. I actually had not thought of that at all when I put this together. The counter argument to using jQuery UI is that it’s good to know how to do it yourself :)

  3. Anton says:
    May 29, 2012 at 10:15 am

    hey pippin, i was just checking out the ‘live search’ example video and noticed a little something that could improve usability – you may have already fixed it but here it goes.
    The live results that appear in the grey box seems to be pushing the rest of the content down.
    i would recommend making that results box position:absolute just in case you have many matches when you’re doing a live search which in turn would push everything off the page.
    Great plugins by the way

    Reply
    • Pippin says:
      May 29, 2012 at 10:37 am

      You’re definitely right, that would be a good improvement.

  4. 5t3ph says:
    May 29, 2012 at 8:05 pm

    Pippin,

    This couldn’t have come at a better time! Implementing this idea for my example app that I’ll use for my WCKC talk :)

    One addition I made was to check if the input value had length >= 2 because otherwise when you delete the field value it lists all users, thanks to the otherwise very useful wildcard.

    Look forward to meeting you this weekend!

    Reply
    • Pippin says:
      May 29, 2012 at 8:23 pm

      Awesome!

      I’ve been looking forward to your talk all week :)

      Checking that the input length is greater than 2 is probably a good idea all the way around. It helps to reduce the number of queries that are made.

      See you soon!

  5. chrismccoy says:
    June 4, 2012 at 7:22 pm

    nice tutorial, i did a live search in my ajax version of twentyeleven ;)

    Reply
  6. jgalea says:
    October 17, 2012 at 9:57 am

    How would you do it if you were loading posts not users? get_users() has the convenient wildcard, but not sure how that would work in the case of searching for posts.

    Reply
    • Pippin says:
      October 17, 2012 at 11:06 am

      I would use WP_Query and pass the search parameters: https://gist.github.com/2023628 (line 166). The posts search isn’t as powerful as the users (no wild card that I know of), but it’s basically the same approach.

    • jgalea says:
      October 17, 2012 at 11:08 am

      Thanks, that wildcard was really useful though, wish we could do something like that with posts.

    • Pippin says:
      October 17, 2012 at 7:36 pm

      That would make all of our lives much easier.

  7. lazerbri says:
    December 11, 2012 at 3:57 am

    hiya very cool but you used url: ajaxurl for use in the admin areas is there a magic url I could use if i wanted a to us on the front end? :) )

    Reply
    • lazerbri says:
      December 11, 2012 at 4:11 am

      ummm I found the answer:) Process Ajax Requests Correctly in WordPress Plugins many thanks you a life saver very cool

Leave a Reply Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Plugin Development 101 – Intro to Actions
  • Using pre_get_posts to Modify Queries for Meta Data and More
  • Let’s Talk Extensible Code
  • Using Ajax in Your Plugin and WordPress Admin
  • Use wp_localize_script, It Is Awesome

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @wptavern Certainly hope that&#039;s the case :) Can&#039;t wait to see what you do
    May 21, 2013
  • RT @photomatt: Secrets Revealed: WLTC and WPTavern - http://t.co/u4ZrXUUgiJ
    May 20, 2013
  • Plugin Development 101 – An Intro to Filters http://t.co/6l2qBm8C8C
    May 20, 2013

Topics

hook the_content featured Tom McFarlin wp_enqueue_script contextual help shortcodes register_setting get_user_meta attachments add_options_page meta box Sugar Event Calendar attachment image Related posts forms mail chimp short codes plugin do_action login authors bbpress apply_filters comments recent posts post types short code taxonomies custom post type Ajax gallery images Stripe taxonomy jquery users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins