It’s easy to simply add your Google adsense for search code as a widget in your sidebar but when you want to place this search bar in certain positions, you may consider changing WordPress default search engine to Google. I redesigned this blog in the last few days, worked on the CSS, re-arranged the widgets and added some little tweaks here and there. I decided to place Google Adsense for search right there on the primary menu, floating to the right. It required a lot of coding I had no idea how to do and found Bill Erickson’s post on adding content to Genesis navigation. For some strange reasons, the code didn’t work for me when I tried adding Google custom search in that position.
I prefer Google Custom search to WordPress search for two reasons:
1. Your visitors get more relevant results
2. You have the chance to monetize your site even more
By going to Genesis > Theme Settings, you can easily display a search bar right there on your primary menu but only WordPress default search can be added in this position. It was then I thought there should be a way to change that default search to Google Search so I could show custom search on the navigation menu. I messed around with the files in Genesis framework and eventually found it.
To do this, you must have created a Google custom search in your Adsense account and have the code ready. It would be great if you make the search result open a page in your site. You can create a page on your site (something like www.doncaprio.com/search) where search results would be displayed.
When creating this page, I think you should select the Full Width Content layout option. You may now login to your Adsense account and create your custom search engine, select the option to display search results on a page on your website, using that page you created. Also, You should only select only your website under site to search.
Got the code? Paste the one for your website on the page you created. Be sure you use HTML editor, not visual.
Now, lets change WordPress default search to this Google Adsense search engine.
- Log in to cPanel and locate this file: …/public_html/wp-content/themes/genesis/lib/structure/search.php
- Download this file first in case something goes wrong or if you wish to revert back to WordPress default search later
- Using cPanel file editor, edit this file and scroll down till you see this piece of code:
' . $label . '
<input type="text" value="' . $search_text . '" name="s" class="s"' . $onfocus . $onblur . ' />
<input type="submit" class="searchsubmit" value="' . $button_text . '" />
- Replace this lines completely with your Adsense search code.
Now, anytime you use the search widget in your Genesis theme, it’s gonna be your Google custom search. To display the search engine like you see on my navigation bar:
– Log in to your WordPress dashboard and go to Genesis > Theme Settings. Under Navigation Settings, check the box that says Enable Extras on Right Side and select Search form.
This automatically places an Adsense for unit on your primary menu. Cool, huh?
According to a very valuable comment left by Patrik, the easiest way to this is by copying the Google custom search code to a text file, name it as searchform.php and upload to your child theme directory.