Create simple Search Form for Google Custom Search Engine

Create Simple Search Form For Google Custom Search Engine. 

A simple search form, less wait. and no need to load any dependencies to display a search form

simple search form
<div class="ht_search_form">
<form action="https://www.example.com/search"><input id="" name="q" type="search" placeholder="Search" />
<button class="ht_search" type="submit">Search</button>
</form>
</div>

This is the Form with an input filed to enter the search query, and a button to submit the query. ( button in not necessary )

two important thing have to notice here

action="https://www.example.com/search"

to where the form have to navigate ( For Dynamic Website Instead of hard-code this value, can use server side language to add dynamically, WordPress / PHP Version added later in this page )

name="q"

"q" is Google Custom Search Engine default search parameter

Now when user search for a term it will navigate to 

https://www.example.com/search?q=search-query

Google Custom Search Engine will get the Search term form URL and show search result

In the Search page where result have to show, Add Google Custom Search Engine code

When using Custom Search box, you may not need the default search box. so select "Result only" or similar at "Look and feel" settings and click on "save & Get Code"

look and feel - result only - cse

The last line, it makes to add the search result only, now Google Custom Search Engine won't Create Search box.

<gcse:searchresults-only>

If you are using WordPress, you can create the search form like above and add in a post or page.

or can change the default search form – form searchform.php

<div class="ht_search_form">
<form action="<?php echo esc_url( home_url( '/search' ) ); ?>"><input id="" name="q" type="search" placeholder="Search" />
<button class="ht_search" type="submit">Search</button>
</form>
</div>

Styles / CSS

This may not be perfect as you wish, but will give some idea

.ht_search_form button {
	color: #000;
	background-color: orange;
}

.ht_search_form input {
	max-width: 200px;
}

.ht_search_form button   –  selects the button 

color: #000;   – change the button text color

background-color: orange;  – change the button background color

.ht_search_form input  – selects the input field

max-width: 200px;  –  make the width of the input field does not exclude 200px

This syntax are added for an idea. based on this can edit the values easily. ( button text color, background color, input filed max width )

simple search form
o/p   ( image )

Add Google Custom Search Engine on you Website

Make WordPress Search Form as Google Custom Search Engine – Search box

Make The Default WordPress Search Form to search using Google Custom Search

WordPress initially check for "searchform.php" to display the search form. ( in child theme or in parent theme ) and this search form can access using "get_search_form()"

For demo purpose we are using Twenty Seventeen Theme

This is the Twenty Seventeen Theme – search form ( searchform.php )

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
	<label for="<?php echo $unique_id; ?>">
		<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'twentyseventeen' ); ?></span>
	</label>
	<input type="search" id="<?php echo $unique_id; ?>" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'twentyseventeen' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
	<button type="submit" class="search-submit"><?php echo twentyseventeen_get_svg( array( 'icon' => 'search' ) ); ?><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'twentyseventeen' ); ?></span></button>
</form>

Notice the form – "action" attribute – it points to the home URL, we can change this

action="<?php echo esc_url( home_url( '/' ) );

and then at the "input" field notice the name attribute – name="s"

name="s"

this is the search field parameter, and this is the default search parameter for wordpress

you can check this by directly entering in URL bar – "youdomain.com/?s=some-query-text

Now WordPress will search for "some-query-text" and display result

"s" is query paramater

Google Custom Search Query default parameter is "q" ( can change this, but if you changed to "s" again default wordpress result may appear, so change to any letter other then "s", "p"   )

To Make the Default WordPress search form to search using Google Custom Search – Change the query parameter "s" to "q"

i.e. the "input" filed "name" attribute value will be "q"

name="q"

and have to add Google Custom Search Engine script in the page where the form action navigate

action="<?php echo esc_url( home_url( '/' ) );

here it navigates to home URL, so add Google Custom Search engine script in home page

or change the form navigation URL to some other page and add Custom code engine script in that page alone.

action="<?php echo esc_url( home_url( '/search' ) );

now create a page "search" and add custom search engine script in that page alone.

Google Custom Search Engine Code

Now When user search on any page – it navigates to "yourdomain.com/search" and will display the result using Google Custom Search Engine.

In Search form if you enter "nyc" then the page will navigate to "yourdomain.com/search/?q=nyc"

Google Custom Search Engine will get the search query term from the URL

You can add the Default search form in sidebar, footer or so . . using Widgets .. which is less wait, and no need to load google script all the time. and when user search's it will navigate to a page. and in that page alone Google custom search engine script will loads


Create simple Search Form for Google Custom Search Engine

Add Google Custom Search Engine in Website

Alternative to Google Custom Search Engine ( CSE )

Google Custom Search ( CSE ) is free product. Its a great tool.

Its easy to setup, just like plugin and play, with in a two minutes you can finish the basic setup and can Add Google Custom Search Engine in your Website

You can Customize Google Custom Search Engine by using API, or from Google Custom Search Engine Admin panel.

but it have some limitations in customization. like Google brand appears on Search result. Display ads on Search Result

Alternate to Google Custom Search Engine

Algolia ( Free plan available ) , Algolia Community build Free Plugins for Several CMS, Which makes easy to Integrate Algolia in your Website . ( WordPress, Shopify, Magento, Jekyll, Zendesk )

Apache solr is open source enterprise search platform build on Apache Lucene

Amazon AWS CloudSearch Fully Managed, Cloud based Custom Search Engine ( for Mobile, Web App )

Swiftype

Elasticsearch

Cludo

AddSearch


WordPress Plugins

Relevanssi replaces the standard WordPress Search Engine with extra features. 100,000 + Active Installation. one of the popular Search Engine plugin in WordPress. Relevanssi premium version

searchwp   –  WordPress plugin

Search Everything Improves WordPress default Search functionality. 90,000 + Active Installations.

Search & Filter

ElasticPress

WP AutoComplete Search


For most use cases Cloud based Search Engines are best instead on all the things loaded on your server.

Cloud Based Search Engines crawl, index your site on there server and manages. means less load on you server. And its easy to Setup, Maintain.

If you have large traffic website. Instead of using WordPress plugin which index you site on your Web Server use Cloud based Search Engine. ( for setup may need to use plugin, even if it is cloud based search engine )

But there are some Advantages of using WordPress based Search Engines

WordPress plugins Search Engines can know how your website content is, they know the popular products like Woo commerce, easydigitaldownloads. and know how they are structured. So more customize feature, better search result.

For Large Enterprises any thing is fine.  Apache solr may be one of the best choose.


Create simple Search Form for Google Custom Search Engine

Add Google Custom Search Engine in Website

Add Custom Search Engine in your Website using Google Custom Search Engine ( CSE )

Create Custom Search Engine using Google Custom Search Engine ( CSE ) and Add that search engine in your website

Google Custom Search Engine ( CSE ) is a tool to implement search engine on your website, or to a separate URL created by google.

Set your own settings. like which pages to search, What Domains to include, Exclude, Include page, look and feel, Image Search, Speech Input, synonyms keywords, Language and much more

Can add other website to you custom search engine. even that website is not owned by you. No need to verify any domain name, to add in Google Custom Search Engine.

First we will see how to setup Google Custom Search Engine, later we will setup some additional features

And we will check what the other things have to do When google Custom search Engine is Setup, later – Tricks / Hacks ..

add google custom search engine on  website

Create Google Custom Search Engine

Navigate to https://cse.google.com/cse/

It will navigate to show already created Search Engine list.

But if this is the first one to create

Click on "Create a Custom Search Engine"  if not, to create a new Search Engine Click on "Add" or click on "New Search Engine" from the left side menu.

Sites to Search

Enter the sites name to search, Can add multiple domain names. can Add domains that even if you are not owned that we.

Individual pages: To search only on some Individual pages – can that page URL e.g. www.example.com/about.html

Entire Site:  To Add Entire Site  e.g. www.example.com or www.example.com/* but this is not including all sub domains, search on all pages with in www.example.com. To search on Sub pages add like sub.example.com or sub.example.com/*

Entire Domain: to search on entire domain – all webpages that include sub folder, sub domains *.example.com

parts of site / sub folders:  www.example.com/blog/ or www.example.com/blog/*

URL Patterns – Google Custom Search

Languages

Add the desired language, Language for search engine Messages to display messages

( this is not the language of what your search result is, or what your website content is. )

This is useful to set the language to display search box. like the search box placeholder – text, sort by – text, powered by google custom search – text  and similar messages.

Name of the SearchEngine

Give a name to your Custom Search Engine,

If you are using Custom Search Engine public URL ( will check about that in this page ) This name shown at the top of that page just like an heading.

Click on Create to create a Custom Search Engine

cse - success message

on Success you will get the page which provide links to "Get Code", "Public URL", "Control Panel"

At Any Time to get this information

Click on "Edit Search Engine" – from the left menu. And then click on "Setup" – This is Control Panel – for Search Engine

cse  - admin - setup

To get the Code – from "Setup"  click on "Get Code"

To get, enable, disable the public URL click on "Public URL".

Copy the code and paste it where you like to display the search box, search result.

e.g  if you like to add search box in Sidebar, If you are using WordPress and have your Theme provide to add content on Sidebar using Widgets.

Then form wp-admin – Dashboard -> Appearance -> Widgets

Select "Custom HTML" Widget and place that widget where you want – sidebar, footer, header ..

and add the code in that widget and save.

Google Custom Search in Sidebar - using custom HTML Widget

We can arrange things like  – Display only Search result or Search box or Seach box at one place and Search result at one place.

To do this – Click on "Look and feel" from the left navigation.

or you change the last line of the code by your self

<gcse:search>   –  To display Search box, Search Result

<gcse:searchbox>, <gcse:searchresults> – Two Colums – To Display Search box, Search result on two columns with in same page.

<gcse:searchbox-only>  –  To Display only Search Box

<gcse:searchresults-only>   –  To Display only Search Result

Instead of manually editing this code, Google will generate this code by changing the settings at "Look and feel"

change look and feel - cse

Customize the color of the link, text, buttons, by navigate to Customize tab, or can change the look and feel using themes.

Synonyms

Add Variations for search terms

e.g. in your website you may use term like "New York City" but user may search for different term like "NYC"

Add synonyms

Now if search query is "NYC" Search Engine will show results that are related to "New York City"

Things May need to do

Make Google Custom Search or Google Search Engine know your Website, Web pages. Add your Site at Google Search console and add sitemap. 
So that Google know what the pages, Content exist on your site and what the result have to display based on the search query.

If you are using Google Analytics – You may be already Added Search Query String in filter "S", CSE default Query String is "q". Make sure you added "q" also. or change the Default Query String. ( in all case you don't need, use this in cases where your URL changes and adds "?q" string  before the search query )

At "Statistics and Logs"  -> Google Analytics. Select a profile to view Analytics.

Don't Enable  – Results Browsing History – just back to previous search result. ( by default disabled )

Tips / Hacks

Instead of adding Google Search Engine Script on all pages,

Just add the custom search box on all pages i.e. create your own search form which is less wait – just an input field, submit button inside form, and When user submit that form make sure to navigate to a page where you added Google Custom Search Engine script. ( have to extend the URL with query string, query text )

or

Instead of creating a new search form, you can use WordPress / theme default Search form, but one issue here  – WordPress search Query String is "s" where default CSE query String is "q". either one have to change to match.

or

In menu add one more item like "search" and make that link to navigate to page where Google Custom Search Engine Script is added. or add a search bar in menu


What about Google Site Search ( GSE ) ?

GSE vs CSE

Google Site Search ( GSE ), is discontinued

GSE is more Enterprise Edition of Google custom Search, can remove Google Branding, can remove Ads. Its not free. ( any how now its discontinued )


Alternative to Google Custom Search Engine ( CSE )

Make WordPress Search Form as Google Custom Search Engine – Search box

Add Search box in WordPress Menu