This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to your site and customize the display to match your website's aesthetics.
Here's how to do it:
JavaScript Code:
google.load("search", "1", {"language" : "en"}); function initialize() { var searchControl = new google.search.SearchControl(); // Site-restricted web search var siteSearch = new google.search.WebSearch(); siteSearch.setUserDefinedLabel("jquery4u.com"); // Replace with your domain siteSearch.setUserDefinedClassSuffix("siteSearch"); siteSearch.setSiteRestriction("jquery4u.com"); // Replace with your domain searchControl.addSearcher(siteSearch); // Add other search types (Web, News, Blog, Image, Book, Video) searchControl.addSearcher(new google.search.WebSearch()); searchControl.addSearcher(new google.search.NewsSearch()); searchControl.addSearcher(new google.search.BlogSearch()); searchControl.addSearcher(new google.search.ImageSearch()); searchControl.addSearcher(new google.search.BookSearch()); searchControl.addSearcher(new google.search.VideoSearch()); // Optional: LocalSearch, PatentSearch // Tabbed display var drawOptions = new google.search.DrawOptions(); drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED); searchControl.draw(document.getElementById("searchcontrol"), drawOptions); } google.setOnLoadCallback(initialize);
HTML Code:
<div id="searchcontrol"></div>
CSS Code:
#searchcontrol, .gsc-control, .gsc-results { width: 600px; } .gsc-input { width: 200px; } /* Increased input width for better usability */ .gsc-branding { display: none; } /* Hide "powered by Google" */ .gs-title a { color: orange; font-weight: bold; } #searchcontrol a { color: orange; }
Remember to replace "jquery4u.com"
with your actual domain name. This improved CSS also widens the search input box for easier typing.
Google Search API AJAX FAQ:
This section answers common questions about using the Google Search API AJAX. Details on implementation, benefits, limitations, commercial use, troubleshooting, customization, optimization, pricing, and support are provided in the original article.
The above is the detailed content of Custom Google Search API Setup Tutorial. For more information, please follow other related articles on the PHP Chinese website!