Welcome to Egypt Forums Mark forums read | Egypt Main Page
Egypt Forums
Arabic Movies



Javascript / VB Script JavaScript and VBScript are interpreted, scripting languages that let you embed simple programs into Web pages. JavaScript/VBScript programs are embedded directly within the HTML document. When the browser loads the HTML document, the programs are also loaded.

Javascript / VB Script Thread, Building a Wordpress / website live Ajax search using jQuery in Web Development; Building a Wordpress / website live Ajax search using jQuery Building a Wordpress / website live Ajax search using jQuery ...

Short Link: http://forum.egypt.com/enforum/showthread.php?t=6887


Reply
LinkBack Thread Tools Display Modes
Building a Wordpress / website live Ajax search using jQuery
 
 
The God Father
Developer's Avatar

Reply With Quote
Thanks: 1
Thanked 212 Times in 154 Posts
 
Join Date: Jul 2008
Location: NDC
Posts: 5,426
02-12-2008, 12:01 AM
 
Building a Wordpress / website live Ajax search using jQuery

Egypt.Com EnForum
Building a Wordpress / website live Ajax search using jQuery

Many websites on the web have allot of content, some even have to much, so that's why a search engine comes in handy! Like Wordpress which has one by default. Just enter your search term and browse true the results! But if you want to make it even better, you build a live search using jQuery! Which search automatically after you entered your keyword!

Egypt.Com EnForumEgypt.Com EnForum
Step 1 - Download jQuery and the required plugin

Before we can start editing our Wordpress theme, we have to download the required JavaScript files. For this tutorial we will use the jQuery JavaScript framework and a plugin called “jQuery Live Ajax Search” which is made by Andreas Lagerkvist.
Egypt.Com EnForum
Just download the Production version because we are not going to edit the jQuery Framework.
The plugin files can be found here: plugin.zip |
Visit plugin author website
Step 2 - Uploading the required files

Now you can use this method for any websites, but in this case we will apply it to a Wordpress theme.
So lets upload the files to our theme directory, in this tutorial, i will use the default Wordpress theme.
Theme Directory: wp-content/themes/default/
Now this is how you need to structure your files to keep it nice and clean:
Egypt.Com EnForum
Make sure you use the same structure for your files.
Which means:
  • wp-content/themes/default/css/live-search.css
  • wp-content/themes/default/js/jquery-1.2.6.min.js
  • wp-content/themes/default/js/jquery.liveSearch.js
Step 3 - Include the files

To include the JavaScript and the CSS we have to edit the header of the template, which is header.php. Here we can find the <head> tags, where the JavaScript and CSS should be loaded.
view plaincopy to clipboardprint?

  1. <link rel=stylesheet type="text/css" href="<?php bloginfo('template_directory'); ?>/css/live-search.css">
  2. <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery-1.2.6.min.js"></script>
  3. <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.liveSearch.js"></script>
  4. <?php wp_head(); ?>

<link rel=stylesheet type="text/css" href="<?php bloginfo('template_directory'); ?>/css/live-search.css"> <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery-1.2.6.min.js"></script> <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.liveSearch.js"></script> <?php wp_head(); ?> This will load the required files, now what means the following:
view plaincopy to clipboardprint?

  1. <?php bloginfo('template_directory'); ?>

<?php bloginfo('template_directory'); ?> This is a Wordpress code that will return the URL to the directory of the theme. Which will return the following:
view plaincopy to clipboardprint?

  1. <link rel=stylesheet type="text/css" href="http://xcodetuts.com/wp-content/themes/default/css/live-search.css">
  2. <script type="text/javascript" src="http://xcodetuts.com/wp-content/themes/default/js/jquery-1.2.6.min.js"></script>
  3. <script type="text/javascript" src="http://xcodetuts.com/wp-content/themes/default/js/jquery.liveSearch.js"></script>

<link rel=stylesheet type="text/css" href="http://xcodetuts.com/wp-content/themes/default/css/live-search.css"> <script type="text/javascript" src="http://xcodetuts.com/wp-content/themes/default/js/jquery-1.2.6.min.js"></script> <script type="text/javascript" src="http://xcodetuts.com/wp-content/themes/default/js/jquery.liveSearch.js"></script> Step 4 - Taking a look at Wordpress search

Before we can start, lets take a look at the search form.
view plaincopy to clipboardprint?

  1. <form method="get" id="searchform" action="http://xcodetuts.com/">
  2. <label class="hidden" for="s">Search for:</label>
  3. <div><input type="text" value="" name="s" id="s" />
  4. <input type="submit" id="searchsubmit" value="Search" />
  5. </div>
  6. </form>

<form method="get" id="searchform" action="http://xcodetuts.com/"> <label class="hidden" for="s">Search for:</label> <div><input type="text" value="" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form> Now when you enter a search keyword and hit Search, you will see that the url changes in your browser address bar:
Egypt.Com EnForum
As you can see your search term will display after the s=, if you would change this you will see that it will look for the term you have changed it to. So this is how it works, i needed to make this clear because it will help you understand the next step.
Step 5 - Configuring jQuery live ajax search

Now its very easy to activate the live search, just open footer.php and add the following before </body>
view plaincopy to clipboardprint?

  1. <script type="text/javascript">
  2. $('#s').liveSearch({ajaxURL: 'index.php?s='});
  3. </script>

<script type="text/javascript"> $('#s').liveSearch({ajaxURL: 'index.php?s='}); </script> Ok great, but what did we just do?
We told the plugin where to grab the search term and where to find the search file.
So it will look for a input field where the id=”s” in this case our search input field. I also showed you the way how Wordpress searches, as you can see i told the live search plugin that index.php?s= is how to search and this means that the plugin will add the search terms after the s=!
So let’s try it out:
Egypt.Com EnForum
Now that’s not really what we want right, because we get the search results back including the whole template. So we need to apply a little hack to make this work. First let’s change the JavaScript to:
view plaincopy to clipboardprint?

  1. <script type="text/javascript">
  2. $('#s').liveSearch({ajaxURL: 'index.php?ajax=1&s='});
  3. </script>

<script type="text/javascript"> $('#s').liveSearch({ajaxURL: 'index.php?ajax=1&s='}); </script> I added a short string which allows us to check if somebody using the normal search, or the ajax live search instead. So now that’s done, open search.php.
Now you see a whole bunch of code, let’s start simple so add the following before <?php get_header(); ?>.
view plaincopy to clipboardprint?

  1. <?php if($_GET['ajax'] == '1'){ ?>
  2. //Our code for live search
  3. <?php }else{ ?>
  4. // Here is the code that's allready in that file
  5. <?php } ?>

<?php if($_GET['ajax'] == '1'){ ?> //Our code for live search <?php }else{ ?> // Here is the code that's allready in that file <?php } ?> So now when you would enter some text:
view plaincopy to clipboardprint?

  1. <?php if($_GET['ajax'] == '1'){ ?>
  2. Hi there! This is going to be our live search!
  3. <?php }else{ ?>
  4. // Here is the code that's allready in that file
  5. <?php } ?>

<?php if($_GET['ajax'] == '1'){ ?> Hi there! This is going to be our live search! <?php }else{ ?> // Here is the code that's allready in that file <?php } ?> Egypt.Com EnForum
So that works perfect! And if you would hit the search button you will see that the normal search also works fine! Egypt.Com EnForum
But now we have to make it so that it shows us the real search results.
So what i did is copy the code from the original search and changed a few things:
view plaincopy to clipboardprint?

  1. <?php if($_GET['ajax'] == '1'){ ?>
  2. <?php if (have_posts()) : ?>
  3. <h2>Search Results for "<?php the_search_query(); ?>"</h2>
  4. <?php while (have_posts()) : the_post(); ?>
  5. <div class="search-result">
  6. <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
  7. </div>
  8. <?php endwhile; ?>
  9. <?php else : ?>
  10. <h2>No posts found. Try a different search?</h2>
  11. <?php endif; ?>
  12. <?php }else{ ?>

<?php if($_GET['ajax'] == '1'){ ?> <?php if (have_posts()) : ?> <h2>Search Results for "<?php the_search_query(); ?>"</h2> <?php while (have_posts()) : the_post(); ?> <div class="search-result"> <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> </div> <?php endwhile; ?> <?php else : ?> <h2>No posts found. Try a different search?</h2> <?php endif; ?> <?php }else{ ?> As you can see i removed parts like <?php get_header(); ?>, <?php get_sidebar(); ?> and <?php get_footer(); ?> because these tags include template files. And we don’t want that! So this is how it would look:
Egypt.Com EnForum
Final: Search.php
view plaincopy to clipboardprint?

  1. <?php if($_GET['ajax'] == '1'){ ?>
  2. <?php if (have_posts()) : ?>
  3. <h2>Search Results for "<?php the_search_query(); ?>"</h2>
  4. <?php while (have_posts()) : the_post(); ?>
  5. <div class="search-result">
  6. <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
  7. </div>
  8. <?php endwhile; ?>
  9. <?php else : ?>
  10. <h2>No posts found. Try a different search?</h2>
  11. <?php endif; ?>
  12. <?php }else{ ?>
  13. <?php get_header(); ?>
  14. <div id="content" class="narrowcolumn">
  15. <?php if (have_posts()) : ?>
  16. <h2 class="pagetitle">Search Results</h2>
  17. <div class="navigation">
  18. <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
  19. <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
  20. </div>
  21. <?php while (have_posts()) : the_post(); ?>
  22. <div class="post">
  23. <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
  24. <small><?php the_time('l, F jS, Y') ?></small>
  25. <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
  26. </div>
  27. <?php endwhile; ?>
  28. <div class="navigation">
  29. <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
  30. <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
  31. </div>
  32. <?php else : ?>
  33. <h2 class="center">No posts found. Try a different search?</h2>
  34. <?php include (TEMPLATEPATH . '/searchform.php'); ?>
  35. <?php endif; ?>
  36. </div>
  37. <?php get_sidebar(); ?>
  38. <?php get_footer(); ?>
  39. <?php } ?>

<?php if($_GET['ajax'] == '1'){ ?> <?php if (have_posts()) : ?> <h2>Search Results for "<?php the_search_query(); ?>"</h2> <?php while (have_posts()) : the_post(); ?> <div class="search-result"> <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> </div> <?php endwhile; ?> <?php else : ?> <h2>No posts found. Try a different search?</h2> <?php endif; ?> <?php }else{ ?> <?php get_header(); ?> <div id="content" class="narrowcolumn"> <?php if (have_posts()) : ?> <h2 class="pagetitle">Search Results</h2> <div class="navigation"> <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div> </div> <?php while (have_posts()) : the_post(); ?> <div class="post"> <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> <small><?php the_time('l, F jS, Y') ?></small> <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p> </div> <?php endwhile; ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div> </div> <?php else : ?> <h2 class="center">No posts found. Try a different search?</h2> <?php include (TEMPLATEPATH . '/searchform.php'); ?> <?php endif; ?> </div> <?php get_sidebar(); ?> <?php get_footer(); ?> <?php } ?> As you can see it returns the post “Hello world!”, so it works great!
Step 6 - Styling search results

We are almost done! We only need to style our results, which we can do in live-search.css. In this CSS file you can change all kind of things for the live search like the width of the results, fonts, colors etc.
First let’s change the margin and padding of h2 and h3 because its way to much:
view plaincopy to clipboardprint?

  1. div.live-search-results h2 {
  2. font: 14px/1.2 Trebuchet MS, sans-serif;
  3. padding: 0;
  4. margin: 0;
  5. }
  6. div.live-search-results h3 {
  7. font: 12px/1.2 Trebuchet MS, sans-serif;
  8. padding: 0;
  9. margin: 5px;
  10. }

div.live-search-results h2 { font: 14px/1.2 Trebuchet MS, sans-serif; padding: 0; margin: 0; } div.live-search-results h3 { font: 12px/1.2 Trebuchet MS, sans-serif; padding: 0; margin: 5px; } So that will look like:
Egypt.Com EnForum
So that’s the end of this tutorial, ofcourse you can style it much nice and give it some nice effects! So if you enjoyed it, please leave a comment and show me what you have made!
Egypt.Com EnForumEgypt.Com EnForum
__________________
I Love Walking In The Rain Cuz Nobody Know I'm Crying !!
 
 
 
Reply

Javascript / VB Script Thread, Building a Wordpress / website live Ajax search using jQuery in Web Development; Building a Wordpress / website live Ajax search using jQuery Building a Wordpress / website live Ajax search using jQuery ...

Short Link: http://forum.egypt.com/enforum/showthread.php?t=6887


Bookmarks

Tags
ajax, building, jquery, live, search, website, wordpress


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[AJAX] Album Next & Prev Links Use AJAX Developer Mods for 3.8.x 0 01-12-2008 11:36 PM
MSN Live 9 BETA - Windows Live Messenger 9 Developer Software and Programs 0 30-11-2008 04:15 PM
[AJAX] Live Search Developer Mods for 3.7.x 0 30-09-2008 02:22 PM
100+ Banners For Your Website Developer Web Application 0 03-09-2008 08:57 PM
Wordpress Affiliate Pro //Plus// Wordpress Linkcloak Developer Web Application 0 01-09-2008 08:18 PM