Home > Ajax, General, JavaScript, Labs > Adding autocomplete field for search in Wordpress

Adding autocomplete field for search in Wordpress

October 10th, 2008

Added a nice Autocomplete field of the search box in that blog /top right/. It’s really nice to have and I really enjoyed doing it.

I followed that article: How to: Create an Ajax-based Auto-completing Search Field for your Wordpress Theme, but instead of using suggested js code, I’ve used Script.aculo.us Autocomplete way.

I really enjoyed and I hope you will like it too.

Update: I’ve wrote a plugin for this: nik.chankov.net/autocompleter/. Enjoy!

Ajax, General, JavaScript, Labs , , , ,

  1. Borans
    October 11th, 2008 at 20:27 | #1

    can you share how you done it with us

  2. October 11th, 2008 at 20:32 | #2

    I followed the article mentioned in the post :)

  3. Borans
    October 11th, 2008 at 20:44 | #3

    i cant make it work, how you include the js in the html file ?

  4. October 11th, 2008 at 20:51 | #4

    Yep this guy didn’t explain this.

    I’ve added the

  5. Borans
    October 11th, 2008 at 20:58 | #5

    can i look your js file? what is the name of it?

  6. October 11th, 2008 at 21:10 | #6

    ok, what I’ve did:
    1. Check if you have prototype included in your html, if not include it with

    <script type='text/javascript' src='/wp-includes/js/prototype.js?ver=1.6'></script>

    2. I added following code into the header.php:

    <script src="/wp-includes/js/scriptaculous/effects.js" type="text/javascript"></script>
      <script src="/wp-includes/js/scriptaculous/dragdrop.js" type="text/javascript"></script>
      <script src="/wp-includes/js/scriptaculous/controls.js" type="text/javascript"></script>

    3. I’ve added this as well, because this is the colorization of the dropdown afterwards:

    <link type="text/css" rel="stylesheet" href="/wp-content/themes/genkitheme-10/gettags.css"  />

    4. and I’ve added this javascript after the form:

    <script type="text/javascript">
    //<![CDATA[
    var contact_name_auto_completer = new Ajax.Autocompleter('s', 'tag_update', '/wp-content/themes/genkitheme-10/gettags.php', {})
    //]]>
    </script>

    Which doing the actual job.

    5. I also changed the second and the third line of the php file, because with scriptaculous the post param was $_POST['s'].

    so instead of:

    if (isset($_POST['search'])) {
            $search = htmlentities($_POST['search']);

    I have:

    if (isset($_POST['s'])) {
            $search = htmlentities($_POST['s']);

    6. if you copy and paste the form from the example above, remove onkeyup=”request(this.value);” callback, because with scriptaculous it’s not needed.

    Hope this is much clearer now.

  7. Borans
    October 11th, 2008 at 21:33 | #7

    Thank you very much Nik

  8. Borans
    October 11th, 2008 at 22:07 | #8

    Thank you very much, one last question nik, there is a bug in the php file that the other guy’s. for example if i write something in the input and delete it, it displays all the tags in the database. but in your site this bug is not exist. how you managed to do it?

  9. October 11th, 2008 at 23:06 | #9

    I think this is because I am using Scriptaculous rather than suggested in the original post. Anyway, this is the content of my php file:

    <?php
    if (isset($_POST['s'])) {
            $search = htmlentities($_POST['s']);
    } else  $search ='';
    $db = mysql_connect('localhost','user','password'); //Don't forget to change
    mysql_select_db('database', $db);            //theses parameters
    $sql = "SELECT name from wp_terms WHERE name LIKE '$search%'";
    $req = mysql_query($sql) or die();
    echo '<ul>';
    while ($data = mysql_fetch_array($req)){
            echo '<li><a href="#">'.htmlentities($data['name']).'</a></li>';
    }
    echo '</ul>';
    mysql_close();
    ?>

    Hope this helps

  10. October 14th, 2008 at 02:58 | #10
  11. November 24th, 2008 at 17:26 | #11

    This is an interesting and useful plugin. Is it compatible with wordpress 2.6.3?

  12. January 8th, 2009 at 06:53 | #12

    Thank for this very usefull code!!!

  13. February 27th, 2009 at 00:04 | #13

    Thank you:) it’s very usefull code:P

  1. No trackbacks yet.

*
To prove that you're not a bot, enter this code
Anti-Spam Image