How to associate your WordPress Blog’s Local Search with Google Adsense Custom Search
This post was originally published at LionVineyard.com on 2009/03/07
Some people like to have one, some people two, and some people two-in-one.
Another task I was faced with recently was to associate the WordPress local search of my blog with Google Adsense Custom Search. That is if you search in the local search and has no hits it will automatically submit the search query to Google Adsense Custom Search and display the results. In this way you can have a hybrid Google Adsense Custom Search and display only one search box on your front page.
The first thing you have to do is to follow the steps in my earlier post of how to add a Google Custom Adsense to your blog.
Now the tricky part starts. The first thing you have to do is to figure out the variable that holds the search string of your local search. Usually it is denoted by “s” and is assigned to the name parameter of the search box input.
<form id="searchform"> <input id="s" name="s" type="text" value="Search" /> <input id="sbutt" type="submit" /> </form>
Once you have that you have to edit the search.php file of your theme. Here you will have to add your Google Adsense Custom Search search box script within the conditional statement in case of no results found.
<!-- // YOUR LOCAL SEARCH RESULTS ARE DISPLAYED HERE -->
<!-- // IN THE EVENT THERE ARE NO LOCAL RESULTS -->
<!-- // HERE COMES THE GOOGLE SEARCH -->
<form id="cse-search-box" action="http://YOURDOMAIN.COM/custom-search"> <!-- GIVE A UNIQUE NAME TO THE FORM -->
<input name="cx" type="hidden" value="given to you by Google - do not change" />
<input name="cof" type="hidden" value="given to you by Google - do not change" />
<input name="ie" type="hidden" value="given to you by Google - do not change" />
<input name="q" size="31" type="text" value="<?php echo $s; ?>" /> <!-- NOTE HERE THAT THE SEARCH STRING OF THE LOCAL SEARCH IS ECHOED TO THE GOOGLE CUSTOM SEARCH BOX -->
<input name="sa" type="hidden" value="Search" />
</form>
<script src="given to you by Google - do not change" type="text/javascript"><!--mce:0--></script>
<!-- // THIS SCRIPT IS NEEDED TO SUBMIT THE SEARCH FORM AUTOMATICALLY -->
<script type="text/javascript"><!--mce:1--></script>
You will only need tou change three things in the search script provided by Google:
- The search form name
- Echo the local search variable to the Google Custom Search Box
- Add a Javascript to automatically submit the form.
This will take care of everything and will return the Google Adsense Custom Search Results on the custom-search page you created.
So in conclusion, with this How-to, you will be able to set up a your local WordPress search to direct to Google Adsense Custom Search if there are no hits within your blog.
You can test it, by searching for something in my local search box.


, or by using our 


