Mobile Number Tracker
Trace Mobile Number Earn Money Online

Use Search Box of WordPress to Send Query to Google CSE

TechWelkin publishes helpful WordPress tips for you!
Samyak Lalit | October 15, 2013 (Last update: April 24, 2021)

Samyak Lalit is an Indian author and disability rights activist. He is the principal author and founder of projects like TechWelkin, WeCapable, Viklangta, Kavita Kosh among many others.

At the onset, let me say that this is not really required to use WordPress Search Box! If you want to use Google Custom Search Engine (CSE), you can and you should use Google’s search box. Using Google CSE search box has several benefits.

However, for whatever reason, if you really want to use the search box of WordPress but perform CSE search through it –you can send search query to Google and get the results through CSE.

This tutorial assumes that you are well-versed with PHP and editing PHP files in WordPress. I recommend that you be careful while editing the code of your WP site. Here, I am giving you only clues –assuming that you know how to go about properly editing theme PHP code.

STEP 1: Create a Custom Search Engine

Go to Google CSE page and create a custom search engine. You can specify which website(s) your search engine should show the results from. You can also customize the look and feel of your CSE search engine and select from several search related features.

In the end, save your settings and get Google code to be embedded into your site to integrate newly create CSE into it.

The code you get from contains three parts:

  1. A JavaScript code
  2. Search box markup ( <gcse:searchbox></gcse:searchbox> )
  3. Search result markup ( <gcse:searchresults-only></gcse:searchresults-only> )
TechWelkin publishes helpful WordPress tips for you!

TechWelkin publishes helpful WordPress tips for you!

STEP 2: Integrate Search Result Markup

Open your theme’s search.php file. In most WordPress themes, PHP file that shows the search results is named search.php –but it in some cases the name could be different. So, look for it and open the file that is responsible for showing search results.

Delete all the code in this file EXCEPT functions that create header, sidebar and footer (i.e. <?php get_header(); ?> AND  <?php get_sidebar(); ?> AND <?php get_footer(); ?>)

After <?php get_header(); ?> paste Google’s JavaScript and search result markup.

This essentially replaces the theme’s code that shows search results with the Google’s search result displaying code.

STEP 3: Change Search Result Markup

Just now you’ve pasted search result markup that you got from Google. Original markup looks like:


Now change this markup as:

<gcse:searchresults-only queryParameterName=”s”></gcse:searchresults-only>

This is it! Save the search.php file

Now, when you’ll enter a search query in your WordPress search box –the results will be shown from Google search and not from WordPress database.

How does it work?

WordPress search box sends the query string with a parameter named as “s”. Luckily, Google CSE allows you to define the queryParameterName. The search.php file now contains Google’s code and this code sends the defined parameter’s value to Google’s server. Google server search your WordPress website for the query and returns the results back to the search.php.

We hope this works for you. As mentioned earlier –please edit the WordPress code if you know what you are doing. Please feel free to ask me if you have any questions on the topic of WordPress search and Google CSE. I will be happy to try and help you. Thank you for using TechWelkin.


4 responses to “Use Search Box of WordPress to Send Query to Google CSE”

  1. Warren Machanik says:

    Hi thanks for this, but my code is not firing. I am using a child theme, and copied the search.php across from the parent theme, but it does not appear to fire. I even renamed the parent theme search.php – still nothing. Any ideas?

  2. Jake says:

    This was a huge help, thanks a ton!

  3. Jay says:

    You made my day man… trying to do it for a month but in vain…only your tutorial made sense to me and you made it look so easy. Thanks a lot.

  4. swapna lekshamanan says:

    Awesome post.. neat and simple

    Thanks a ton :)

Leave a Reply

Your email address will not be published. Required fields are marked *