Integrate Google Custom Search Engine (CSE) in your WordPress Blog
Not fond of the built-in Wordpress engine?
Let’s face it, while the built-in WordPress search simply works, there’s something magical about the Google search engine.
In this article, I’ll be showing you how to integrate the Google Custom Search engine kit in your WordPress blog by replacing the default search engine, and how to do it right the first time. You’ll need to arm yourself with a little more than an hour to get going, and a little patience.
Sign up for a Google Custom Search Engine
Simply jump on over to the Google CSE home page and sign up. Once you’ve filled in the required information, head into your Control Panel and select Get Code. Choose Host results on my website using an iframe and in the Result details box, enter your website’s address followed by /search. For example, mine would be:
http://www.steelfrog.com/search
NOTE: Select the Top and bottomvalue for ads placement. This will place the ads on the top and bottom of the search results rather than on the sides, reducing the minimum required width of your iframe to 500px.
Set up the WordPress Search Template
If it’s not there already, create a blank search.php document and upload it into your WordPress theme’s directory.
/wp-content/themes/[theme]/search.php
It should appear in your Theme Editor sidebar as Search Results. Copy and paste the information found on your Google CSE Control Panel. It should be similar to the one found below:
<div id="cse-search-results"></div>
<script type="text/javascript">
var googleSearchIframeName = "cse-search-results";
var googleSearchFormName = "cse-search-box";
var googleSearchFrameWidth = 500;
var googleSearchDomain = "www.google.com";
var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
Include this code on top of your Google CSE code. This defines the page as a WordPress template file which we’ll need to use later:
<?php
/*
Template Name: Search
Description: A Google CSE template.
*/
?>
Setup a Search Page
We now need to setup a search page. We need to have both a template and a single page for this to work. In your WordPress Pages tool, select Add New. You can leave the page blank but it is vital that you check two values:
- The permalink is set to www.yoursite.com/search/
- The page uses the Search template we’ve previously setup
Make sure that the permalink path to this page is yoursite.com/search. If it is not, you are either using other forms or URL formatting. You can change that setting in your Settings panel, under Permalinks. I prefer to use the name value, as follows:
/%postname%/
You can edit the page’s permalink by clicking on its box.
We also need to make sure that our Search page is using the Search template we’ve setup. On the right-hand side, scroll down until you see the Template section and select the Search template. This will tell WordPress to use the Search Results page we’ve created earlier, which contains the results code. Once you’ve double-checked both these options, can can simply save the page and continue.
The Search page does not require any content. The only reason we’ve created a single page is so we can use the yoursite.com/search address, rather than pointing to yoursite.com/wp-content/theme/search.php.
Add the CSE Search Box
Now that our search results page is in place, we need to create our search box. In most cases, all you need to do is replace the content of your template’s Search Form page with the search form provided by Google, similar to this one:
<form action="http://www.yourwebsite.com/search" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="partner-pub-YOUR ID" />
<input type="hidden" name="cof" value="FORID:11" />
<input type="hidden" name="ie" value="ISO-8859-1" />
<input type="text" name="q" size="31" />
<input type="submit" name="sa" value="Search" />
</div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script>
NOTE: Please make sure to back up your theme’s search form in case the Google search form does not comply to your theme’s style. You may need to customize the form’s CSS to suit your website. I’ve heavily customized mine to match my website’s theme.
Try it out!
Save your settings and test your new search form. Make sure you can access the form by going to www.yoursite.com/search.

NCJames
July 23, 2010 at 3:49 PMhttp://www.steelfrog.com/integrate-google-custom-search-engine-cse-in-your-wordpress-blog/#comment-10
Great post! :D
sanjeev kumar gadamsetti
September 1, 2011 at 5:16 PMhttp://www.steelfrog.com/integrate-google-custom-search-engine-cse-in-your-wordpress-blog/#comment-632
this works! and thanks! it is as simple as that.