How to Integrate Google Search in Your WordPress Blog
April 2nd, 2010 by
While there are some nice plugins to enhance WordPress search like Search Everything and Search Unlished, Google custom search engine (AKA Google CSE) seems to have the best algorithm yet.
Before integrating the custom search engine with your blog, I recommend testing it on a local WordPress installation (if you don’t have one, read this), before implementing it on your live blog.

Let’s see how to integrate Google CSE in WordPress:
Preparing your blog for Google custom search integration
First, we need to prepare two files for the integration:
- Go to your WordPress theme folder and look for a file called searchform.php. if it doesn’t exist, create it.
- Create a new page template named searchResults and save it as a new file called searchResults.php.
- Login to your blog dashboard (www.yourblog.com/wp-admin).
- Create a new page (Pages -> Add New) and name it Search Results (or any other name you would like).
- As your page template select searchResults
- Click on Publish
Your searchResults.php should look more or less like this:
<?php /* Template Name: searchResults*/?>
<?php get_header();?>
<div class="post">
<h2><?php the_title();?></h2>
<div class="postContent">
/* leave space here to paste the search results code later on*/
</div>
<?php get_sidebar();get_footer();?>
Creating a custom search engine for your blog
The next thing to do is create and customize your new search engine.
If you don’t have an account in Google (I assume you do, but just in case), create one here.
Go to Google CSE, and click on Create a Custom Search Engine.
On the next screen fill out the following fields:
- Enter the desired name for the search engine (can be your domain name).
- Enter the search engine description (can be the same as your blog’s meta description tag).
- Make sure Only Sites I Select is selected.
- Type your domain name without http (e.g. www.yourblog.com).
- Make sure the Standard Edition option is selected.
- Go over the terms of service (if you know anyone who actually read the entire document, I’d like to know them too) and tick that box.
- Click Next.
- On the following screen click on Finish.

Setting keywords
You should now see your new search engine listed. Click on Control Panel.
- Click on Basics on the left pane.
- Enter Keywords for your search engine (can be the same as the keywords in your blog’s keywords meta tag).
- Click Save Changes.

Customizing the look and feel for the new search engine
After setting the keywords, it’s time to customize the look and feel of the search engine:
- Click on Look and feel on the left pane.
- Select iframe. This means that the search results will be displayed on a separate page (which you already created and named it searchResults.php, remember?)
- Select an option from the list and click on the customize button. However, you can also have your own design for the search box as I did in this blog; in this case, just select the Default option (in addition, check out these tips).
- Click on Save .
- If you want to, you can make money by people clicking the ads in your search results page. To that click on Make Money on the left pane and follow the instructions.
- Click on Get Code .

Copying the code for the search box and the search results to your blog
Alright, now we can copy the generated code to the files we created in WordPress.
- Make sure Get Code is selected in the left pane.
- Enter the URL (permalink) of the page you created in WordPress for your search results and select Top and Bottom for the ads location (refer to these the second tip for the reason to this location).
- Copy the code in the first box and paste it to the searchform.php file in your theme folder (if this file has some code in it already, delete it first and then paste the new code).
- Copy the code in the second box and paste it to the searchResults.php file, where you left some space for pasting (should be line 7 according to that example).

We are done with the integration. However, you can modify those settings at any time and then recopy that code to your files again.
Finally, a few tips:
- The code for your search box can be customized according to your CSS needs (add IDs or classes to the Form and Input tags).
- If you want the search results iframe to have a width less than 795, you need to select Top and Bottom for the ads location.
- If you do the integration locally before going live (recommended), the height of the search results iframe will always have white space below the results. This space will disappear once you
implement the CSE in your live blog. - If your blog’s background isn’t white (like in this blog), wrap the code for the search results with a div. than give that div some right and left padding, so the search results will not appear that close to the edge of the white area. Search this blog to see an example of it.
Feel free to speak your mind or ask questions in the comment area!
Additional resources
Respond Here
35 responses
Trackbacks and Pingacks
- How to Integrate Google Search in Your WordPress Blog | WebTechWise | WpMash - WordPress News
- How to Integrate Google Search in Your WordPress Blog | WebTechWise | WordPress News
- 180+ Got to Check Out Fresh Articles for Designers and Developers | tripwire magazine
- Best 10 WordPress Articles on MMO Social Network
- Avoid error messages when creating Adsense for search | Emerald Interaction

Nice..I have already Integrated Google Custom Search in my blog.
By The way you have got FANTABULOUS Theme!!! awesome!
Thanks Chethan! it’s good to here you like it
hi Chethan,
your google search looks not bad
I have been needing to add a search field/page on my site for a while and haven’t gotten around. This looks like a good walk through to try.
I know, I did the integration very recently either. Anyways James, your blog has many posts in it, therefore I think it will boost the user experience by adding another way to find them.
Thanks Omer – Coincidentally I was trying to implement CSE last week but didn’t have much progress. I’ve done better with your fabulous detailed instructions but I’ve got a couple of issues.
1) It throws out the sidebar so that starts below the results.
2) I’ve got the following error code (possibly related to the 1st issue).
I could try to hack this but any clues would be much appreciated.
Here’s the link to a search result:
http://www.superguide.com.au/search-results?cx=014094977045823341518%3Act9dshcvnzk&cof=FORID%3A11&ie=UTF-8&q=non-concessional+contributions&sa=Search&siteurl=www.superguide.com.au%2F#1410
Many thanks in advance.
Cheers, rob
Oops – here is the error I’m receiving:
Warning: require_once() [function.require-once]: Unable to access /f2/superguide/public/wp-content/themes/default/sidebar.php in /f2/superguide/public/wp-includes/theme.php on line 996
Warning: require_once(/f2/superguide/public/wp-content/themes/default/sidebar.php) [function.require-once]: failed to open stream: No such file or directory in /f2/superguide/public/wp-includes/theme.php on line 996
Small update – It’s actually now not showing the sidebar at all.
Hi Robert, thanks for your comment. Maybe the error you receive is due to strict permissions for your sidebar.php file? the permissions should usually be set to 664 for files and 775 for folders.
In addition, the second message is strange because it seems like that directory does not exist, which means the path is incorrect.
I recommend you double check that path. If it doesn’t work for you, check this http://bit.ly/bIlF9k to see if you find anything useful.
Tell me if it helps. Good luck
Hi Omer – Thanks for your help. I got in touch with Justin Tadlock who developed my theme (the excellent Hybrid theme) and he solved it – it was because I should have based the searchResults.php on the page.php of my theme. More information in this thread: http://themehybrid.com/support/topic/pleace-default-form-to-google-search-box
Cheers, Rob
I could not read Justin’s comment (being available for exclusive members only), but I guess that this theme uses page.php as the base file for search results.
Anyways, I’m glad to hear it works now Robert.
Cheers
I noticed that when you remove the js from the form code, it no longer displays the custom google search logo. Besides that, how does it effect the performance of the site overall?
Hey Tony, thanks for commenting. There is an effect on site performance because you load one less js file for each and every page in your site.
The less HTTP requests you have, the faster is your site.
However, if you want that logo to appear, you can include that js line. If everything else in your site is optimized, it should not matter that much.
Omer, your making me work too hard.
I implemented the Google search on my site. It works great and I’ve noticed that the search results page is now getting views so I know it’s working. I would love to integrate the search into the lower right of my header. Right now it’s on the sidebar. I have very little coding experience and use sharepoint for my html help. If you have any suggestions that would be great. Great post!
Thanks David. To integrate your Google search in lower right of your header (like it’s here), you need to open your header.php theme file and place that the call <?php get_search_form(); ?> where you want it to appear in the header (for example in an LI tag like it is here).
Then remove this call from your sidebar.php file.
BTW, I see you header is a wide image. why not leave that <?php get_search_form(); ?> in your sidebar, but place it above the video, so it will be at the very top of your sidebar?
Thanks for this useful article.
i am happy to find it thanks for sharing it here. Nice work.
thank you very much. the steps are very clear.
Finally I managed to implement custom google search
Great tutorial and I allmost get it to work. The problem is that the google ads are showing on the right side even though I selected top and bottom. Don’t know if I have done anything wrong or if it’s google.
Any help is appreciated
Link to search:
http://www.webbplatsnu.se/sok/?cx=partner-pub-7130525313502612:75u2goepxp7&cof=FORID:10&ie=ISO-8859-1&q=billig+logotyp&sa=S%C3%B6k&siteurl=www.webbplatsnu.se%252F
Marcus, it seems that Google adds appear both on the top and to the right. If you change the preferences to diable Google ads, do they both disappear??
Great tutorial!One question.How did you get your search results to open in this same page?
Norm, take a look at “Customizing the look and feel for the new search engine” step 2.
Thanks for assisting me to integrate search results into my word press blog. I really appreciate you.
With pleasure amaechi.
Thank you for this step-by-step instruction. Looks like I can do it though I don’t have much experiences in web programming.
Will ask you later if any problem occurs.
Omer,
Great post! Thanks! On my wife’s blog the search box is in the header.php file. Do I put the code for the searchform.php file in the header.php file instead?
Dave, the answer is yes, if your theme has the search box code inside header.php and not in searchform.php, you need to paste Google’s code in header.php.
Hi – very detailed instructions there. Been looking for a while!
One question: Removing the watermark seems to be against ToS for google CSE. Any idea about that?
They also seem to be very keen that we don’t modify the code – apart from the one CSS they allow you to use.
Hope you already know these issues..
@Cen – feel free to ask any question.
@Matt – I had no idea about these issues, but after doing a quick check in Google forums it seems you are right. I will have to do that change here. Thanks for that.
Thank you so much! Instructions here are clear and easy to follow. I did exactly that and I’ve embed the GCS in my showcase gallery site!
Great Yahya! I’m glad you got it to work.
I used your tutorial / illustration today on one of my sites after having gone through a couple of others which were okay but did not really help much.
In my opinion what set your post apart from the others is the visual aid you gave, but very definitely also the tips. I was so concerned that I was not allowed to edit the code and customize the look and feel more toward what will work in my site.
Thank you so much for adding those tips, today they were especially helpful to me
(Oh… lol, and the width tip was really handy too, thanks)
Hi Janita,
A bit late on the response, but I wanted to thank you for the good words. I’m really glad to hear this tutorial helped you.
Hi thanks for the detailed and easy info. I am unable to understand step 2 i.e Create a new page template named searchResults and save it as a new file called searchResults.php.
I don’t know how to make page template. moreover please have a look at the website
http://www.newsearn.com/
At the main page there is already at the right bottom a google search box. can we put the code in that. please explain a little more to add google search in the news earn website.
Hi,
I’m not sure regarding the search box you already have there, need to look at the code.
Regarding creating a page template, here’s a straight forward tutorial.