Google Custom Search

The next step was to add Google Custom Search to the website. This involves adding a search box to the header and a page for the results to be displayed. This guide has the detailed instructions which I used.

After creating the custom search engine with Google, I copied the search box code to the header.php file. The form action must point to the results page.

Create a new page with the WordPress admin interface and copy Google’s provided code for the search results into the content. Edit the permalink as necessary, maybe to something simpler than “search-results”, then publish the page.

WordPress will activate its search function by default if the URL is domain/search/[search-term]. For consistency, this should be redirected. The guide says to copy the redirect code provided to index.php. However, I wanted to minimize the number of overridden files if possible. The code works just as well in header.php as the other files call get_header() anyway. What it does is to return the http header with a new location which points to the custom search page if the function is_search() returns true.

One final step. The 404 page displays the default search form. Adding the following code to functions.php will make the default search form disappear. This tip was obtained from wpbeginner.

add_filter('get_search_form', create_function('$a', "return null;"));

That’s it! The power of Google search on the site.

Advertisements

One thought on “Google Custom Search”

  1. That is a really good tip particularly to those new to the
    blogosphere. Short but very precise info… Thanks for
    sharing this one. A must read article!

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