How to Add Google Search in a WordPress Site

As you may have discovered, in spite of its phenomenal performance as a blogging platform/CMS, WordPress falls woefully short in the search engine department. There are of course certain plugins available that will improve your search results, but for those of you who don’t want to install 15 of them to just barely imitate the functionality of modern search engines, there is another option. In this tutorial we are going to show you how to replace WordPress built in search with Google custom search.

Create Custom Search Engine

The first thing you need to do is go to http://www.google.com/cse/. Once there, select “Create a Custom Search Engine”. Enter a name and description first

name and description

And then insert the URL of your website.


Specify Search Details

Once you have done this click “Next”. This will bring you to a page called “Try It Out”. Skip this page and click “Next” again. On the following page click “Change the look and feel”. Select “Two Page” and then click “Save & get code”. Once again, enter your website’s URL. For the query parameter use “s” because this is what WordPress uses to search the database.

specify search

Copy and Paste Code

The first box of code is what you want in your searchform.php file. If you do not have one, go ahead and create it. If you do have one, replace its contents with this code. The second box of code is what you want in your search.php file. Once again, if it doesn’t exist in your theme go ahead and create it. Otherwise just replace the contents of the loop with this code. This means everything between if ( have_posts() ) : and endif.

code boxes

And that’s it, you’re done! Thanks to Google’s algorithms, now every search bar on your site should direct your visitors to a search page populated with results ranked according to relevance rather than post date or something equally frustrating.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s