If you would like to replace the default WordPress search with Google custom search here is the method I use. This implementation of Google custom search is reliant on using the Genesis Framework.
1. Create a Google custom search engine
Go to http://www.google.com/cse and create a custom search engine for your site. Make sure that the look and feel layout setting is set to Results only as shown in the image below.
2. Add the following 2 code sections to your genesis child theme functions.php
Customize the search form text
add_filter( 'genesis_search_text', 'wpselect_search_text'); function wpselect_search_text() { return esc_attr__( 'Google Search ...', 'genesis' ); }
Customize the search form
add_filter( 'genesis_search_form', 'wpselect_search_form', 10, 4); function wpselect_search_form( $form, $search_text, $button_text, $label ) { $onfocus = " onfocus="if (this.value == '$search_text') {this.value = '';}""; $onblur = " onblur="if (this.value == '') {this.value = '$search_text';}""; $form = ' <form method="get" class="searchform search-form" action="' . home_url() . '/google-cse/" > ' . $label . ' <input type="text" value="' . esc_attr( $search_text ) . '" name="q" class="s search-input"' . $onfocus . $onblur . ' /> <input type="submit" class="searchsubmit search-submit" value="' . esc_attr( $button_text ) . '" /> </form> '; return $form; }
3. Create a WordPress Page Template
This page template needs to be created in a new php file in your genesis framework child theme directory. This php file can have any file name, but I use page_google_search.php
You will need to insert the JavaScript code provided by your Google custom search engine in the code below where it says INSERT GOOGLE CUSTOM SEARCH JAVASCRIPT CODE HERE.
<?php /** * Template Name: Google Custom Search * Description: Google Custom Search */ add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' ); remove_action( 'genesis_post_content', 'genesis_do_post_content' ); add_action( 'genesis_post_content', 'wpselect_google_cse_content' ); function wpselect_google_cse_content() { ?> INSERT GOOGLE CUSTOM SEARCH JAVASCRIPT CODE HERE <?php } genesis();
4. Create WordPress page using the Google Custom Search template
Finally you need to create a new WordPress page using the Google Custom Search template. This WordPress page needs to have a permalink of google-cse. If you would like to use a different permalink you can change the code in Customize the search form above.
You should now be able to use the standard WordPress search widget and the search will be forward to the Google custom search results page created above.
Hi,
I have tried your guide, but I can’t get it to work.
I have putted the page_google_search.php in my child theme news. I also went to google custom search and picked results only.
I pasted the javascript function….. where it said INSERT GOOGLE CUSTOM SEARCH JAVASCRIPT CODE HERE
And I have added the step 1 and 2 to news functions.php file.
Is there anything I’m missing?
LikeLike
Did you create the page with a permalink of google-cse?
LikeLike
I did this, so when I make a search now i get redirected to the page where the results should be, but there isn’t anything. Only the title of the page i made, it doesn’t even say “No results for bla bla”
Should i also add something to the page itself?
LikeLike
Did you select the google custom search template for the results page? The template was created in step 3 and you should have substituted the javascript code provided to you from the google custom search engine you created.
LikeLike
Great, thought it was more of a technical workaround – but that was surely easy. Thank you! 😀
However, why is the search result both showing on the page, but also as above the header?
LikeLike
You probably have the “<gcse:searchresults-only></gcse:searchresults-only>” code listed twice in your template. Where it says “INSERT GOOGLE CUSTOM SEARCH JAVASCRIPT CODE HERE” you should paste everything except the above section of code. Sorry for the confusion, I will update the instructions to clarify this. This should solve your issue.
LikeLike
I am using Genesis Framework. I would like to use Goolge custom search for my site. Did all steps that you showed on your site. I am still getting an error message “Not Found, Error 404” Did I miss something? Here is my http://www.ibiology.org. Thank you for your help.
Lobsang
LikeLike
It looks like your site has a different permalink structure that adds .html to your permalinks. You need to change the code in step 2 above to customize the search form and on line 6 change /google-cse/ to /google-cse.html That should work.
LikeLike
Thank you so much for your help. It seems to work. Does Google have to index the site in order for the search to work? I searched for a word that I know exists on the site but it says No Results. My site is not indexed on Google yet.
Thank you,
Lobsang
LikeLike
I am using genesis theme and eleven40 as child theme for my website. I tried that code, it worked on Chrome and Firefox for me but not on IE and safari.
LikeLike
I’m not sure what the issue is. I have tested on IE and Safari and it seems to work for me.
LikeLike
Great instructions!!! Worked perfectly!
LikeLike
Well, I thought it worked perfectly but now I’m getting redirects:
Redirect Notice
The previous page is sending you to an invalid url ().
If you do not want to visit that page, you can return to the previous page.
Any thoughts?
LikeLike
Do you have an example of the URL it’s trying to send you to?
LikeLike
http://www.travelinsurancereview.net/ is where the search button is on the bottom right side. If you click on one of the results in the search results page you will see the redirect error. Thanks!
LikeLike
I tried with both IE and Firefox and it works for me. I wonder if it’s a caching issue, maybe try clearing your browser cache or testing with a different browser.
LikeLike
OK. I now see your issue. It looks like a google custom search issue. I’m not sure how google custom search is generating the URL.
LikeLike
Thank you! I’m using the script the client set up so maybe I should check on how the client set it up.
LikeLike