Main Menu
Your email has been received and will be responded to as soon as possible.
We are available 24/7 to take your call.

Our Blog

30 March 2022
ioadmin

Enable Auto-Complete Search in WordPress Blog!

“One sometimes finds what one is not looking for”

I am pretty sure that you are aware of Google Search Suggest and Yahoo Search Assist features. This feature helps you effortlessly find exactly what you’re looking for. No doubt, it lists a few suggestions to keep the user looking for more and more.

As you type something in the search box, it automatically offers search terms and phrases in real-time. How nice will it be to have the same feature on your website or blog. In this post, I am going to explain an easy way to add a search suggestion feature to your WordPress blog or website.

Things You’ll Need

jQuery: You can download the latest version from jQuery.com
Autocomplete Plugin: Download this plug-in by Jörn Zaefferer from //bassistance.de

Now let’s play with some code

Include this code within <head>..</head> tag.

<script type=text/javascript src=js/jquery.js></script>
<script type=text/javascript src=js/jquery.autocomplete.pack.js></script>
<link href="js/jquery.autocomplete.css" rel="stylesheet" media="screen" type=text/css />

Here I am assuming you are using “js” folder to keep all your java scripts and supported files. You may need to update the location of these files based on your folder setup.

Here’s the actual jQuery code to enable this feature on an input box:

<script type=text/javascript>
    var data = "Search Terms Separated With Spaces".split(" ");  
    $("#myInputBox").autocomplete(data);
</script>

Variable “data” hold the terms that you would like the user to see in the search suggestion list and $(“#myInputBox”) is the id of the search input box.

Let’s check out how we can automatically call WordPress tags in our search suggestions, using WordPress wpdb class:

<?php 
    global $wpdb;
    $search_tags = $wpdb->get_results('SELECT name FROM $wpdb->terms');
    foreach ($search_tags as $mytag){
        echo $mytag->name. ' ';
    }
?>

Here I am using WordPress wpdb class to fetch all tags assigned to the posts.

Here’s the complete code to enable this feature on your WordPress blog. Just replace the folder name with actual folder and the jQuery selector $(“#myInputBox”) with the actual id of search input box.

<script type=text/javascript src=PATH_TO/jquery.autocomplete.pack.js></script>
<link href="PATH_TO/jquery.autocomplete.css" rel="stylesheet" media="screen" type=text/css />
<!-- This goes in the head -->
<script type=text/javascript>
	$(document).ready(function(){  
		var data = '<?php global $wpdb; $search_tags = $wpdb->get_results("SELECT name FROM $wpdb->terms"); foreach ($search_tags as $mytag){ echo $mytag->name. " "; } ?>'.split(" ");
		$("#ID_OF_SEARCH_INPUT_BOX").autocomplete(data);  
	});
</script>

Don’t forget to add your inputs in the comments section below.

Contact Us

Let’s Check Our Portfolio

Leave a Reply

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

Request A Quote Now

Enquiry Now

When We Work Together

We can create something incredible

arrow
HQ INDIA
HQ INDIA
C-31, Milap Nagar,
Uttam Nagar, New Delhi,
Delhi 110059
USA
USA
6715 Backlick Rd Suite 202
Springfield,
VA 22150, USA
AUSTRALIA
AUSTRALIA
2/51, Lane Cres,
Reservoir, VIC
3037, Australia
CANADA
CANADA
61 Payzant Bog Road, Falmouth, NS, B0P 1P0, CANADA
UK
UK
3rd Floor, 131 City Road, London, EC1V 2NX, United Kingdom
UAE
UAE
Boutik Mall, Al Reem Island - Abu Dhabi, UAE
X

Let Us Call You Back

  • India+91
  • United States+1
  • United Arab Emirates+971

Your phone number is kept confidential
and not shared with others.