Adding A Google AdSense Custom Search Engine To a WordPress Blog

Last week, I talked about why I like Google AdSense for Search and offered some setup tips. This week, I’ll show you how to integrate this code into a standard WordPress blog.

Note: You’ll need to know HTML to complete this exercise, and be basically familiar with the WordPress theme editor (found under the “Appearance” menu). I would rank this as an “beginner/intermediate” level tweak. You can’t really screw anything up as long as you have a backup of the original search form, so there’s no harm in trying.

P.S. – This won’t work for WordPress.com users.

P.P.S. – The written tutorial was updated as of November 6th, 2011. If you find that the code doesn’t exactly match, you may still find the instructions useful. Conceptually, the process is the same regardless of the exact code.

Step 1 – Grab the Google Adsense for Search Code

Follow the directions from last week’s post to grab Google’s code (btw, when I say “grab” the code, I mean copy it into a text editor).:

<form action="http://www.google.com/cse" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="partner-pub-xxxx" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" size="55" />
<input type="submit" name="sa" value="Search" />
</div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>

Let’s break this up line-by line:

<form action="http://www.google.com/cse" id="cse-search-box">

This is one of the most significant pieces of code, because the form id – “cse-search-box” – is referenced by Google’s watermark javascript (the two have to match) and because it tells us the form’s “action” is on Google.com.

<div>

This tag – and the closing </div> tag to match it – are nothing more than a placeholders. You don’t need them to comply with Google, so we’re going to ignore them. If your theme’s existing search form has DIV tags, copy them over and ignore the Google DIVs.

<input type="hidden" name="cx" value="partner-pub-xxxx" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" size="55" />
<input type="submit" name="sa" value="Search" />

The first <input> tag contains your AdSense publisher ID – “partner-pub-xxxx.” I’ve changed the code out so that no one gets confused, but the XXXX will represent your specific ID. The second <input> tag is nothing more than a character set declaration. The third <input> tag is the search query box itself. Google defines the length of this form as “55″ characters, and names the box “q”. Finally the last <input> tag is the code that creates the “Search” button we click on.

<script src="http://www.google.com/cse/brand?form=cse-search-box&amp;lang=en" type="text/javascript"></script>

Last but not least is the javascript watermark script. The script src tag has a dynamic variable portion that we need to pay attention to later. For those who don’t know, that’s the portion of the javascript source URL prefaced by a ‘?’ (question mark) but before the “&”, i.e. “?form=cse-search-box”.

Grab Your Blog Theme’s Search Form Code

Because of all of the themes available on the market, this portion may not match your particular website. I’m going to use the simplified search form found in the Twenty Eleven theme because it’s packaged with the basic WordPress install and also a popular theme on WordPress.org.

Under your blog’s appearance menu, you’ll see a link for “Editor.” The editor will open to the active theme’s main CSS stylesheet, and on the right side of the page you’ll see anywhere from 10-20 file names listed. You’re looking for the one that’s called “searchform.php” or “Search Form.” Here’s the searchform code from Twenty Eleven:

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label for="s" class="assistive-text"><?php _e( 'Search', 'twentyeleven' ); ?></label>
<input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
</form>

I’m not going to break-down every line (because there’s some duplication), but I *am* going to point out the key differences below:

1. Change your theme’s search form “action” and “method” to match Google’s code.

The opening “form” tag for Twenty Eleven reads like:

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">

We need to change the action to match the google form code’s action and we need to delete the method="get" portion.

Change the search form's method, action, and id.

Change the search form's method, action, and id.

The key here is to change the method and action of your theme’s existing search form while preserving the existing CSS style. We don’t want to change your form’s class or id settings.

2. Fix the form id so it matches up with the javascript variable.

If your theme’s search form does not have an “id” setting, all you need to do is add id="cse-search-box" inside the opening form tag.

You’ll recall that the javascript tag at the end of Google’s form code references the form id. SO, if your theme’s form already has an “id” tag, you’ll need to change google’s javacscript to match. For the Twenty Eleven theme, you need to change the variable in the javascript code. The final code will look like this:

<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchform&amp;lang=en"></script>

3. Add the hidden inputs from Google’s code to your form code.

This is easy – just take the two “hidden” inputs (one with your publisher ID, one that identifies the character set) and just add them to your theme’s form.

Add the two "hidden" inputs

Add the two hidden inputs

4. Name the search box “q” and the submit button “sa”.

In Twenty Eleven, the search box is already named “s” and the submit button is named “submit”, but we need to change them. In this particular instance, changing the names of the search box and submit button will not impact the look of the form. However, changing the name in some themes will cause the form to change appearance. The solution is to change the CSS so that it applies to the new input name.

If your theme assigns style to these elements by name, simply search your theme’s CSS file for the name and change the name on both the search form and in the CSS. In Twenty Eleven, the search form input has style assigned to the ID “s”, but since we’re not changing the id (just the name), we don’t have to worry about this. As for the input button, we’re OK here too. The style is assigned to the id “searchsubmit,” and since we’re not changing that ID, we don’t have to worry about that either. Still, you should double-check the CSS references for yourself.

5. Add the javascript after the closing <form> tag.

Add the "script" tags after the closing "form" tags

Add the script tags after the closing form tag

Easy.

The final code:

<form id="searchform" action="http://www.google.com/cse">
<label for="s" class="assistive-text"><?php _e( 'Search', 'twentyeleven' ); ?></label>
<input type="hidden" name="cx" value="partner-pub-xxxx" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" class="field" name="q" id="s" placeholder="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
<input type="submit" class="submit" name="sa" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchform&amp;lang=en"></script>

When you’re all done, your theme’s search form will look the same way as before, but it will use Google’s AdSense for Search rather than the limited search engine provided by WordPress. Enjoy!

Comments

  • Dennis Marshall Jun 26th, 2010

    I have a theme called intrepidity and I tried this. Ended up losing my search box all together. Then I just installed the actual google search code. Still nothing. Im not giving up though. I know it’s possible. Really appreciate you post.

  • Dennis Marshall Jun 26th, 2010

    It didn’t work at first but just installing the actual code to my searchform.php put the google search bar in the same spot. The reason it wasn’t working before is because my google search was inactive. I wish they would let me delete inactive ads.

  • admin Jun 26th, 2010

    Dennis – First, you probably want to go back to the original searchform.php file. Once you’ve done that, try going line by line and changing one thing at a time. That way, you can find whatever change is breaking things.

  • Mario Sep 12th, 2010

    I used snowblind theme. I tried your tuts to noavial. Would you tell me where to change what. The default search form is as follow:

    <form method="get" id="searchform" action="<?php bloginfo(‘url’); ?>/">
    <input type="text" class="search_input" value="search…" name="s" id="s" onfocus="if (this.value == ‘search…’) {this.value = ”;}" onblur="if (this.value == ”) {this.value = ‘search…’;}" />
    <input type="hidden" id="searchsubmit" value="Search" />
    </form>

    Thanks in advanced

  • admin Sep 14th, 2010

    Check out this demo video I made if you have more questions:

    http://www.youtube.com/watch?v=Pp0VW7Ak6_0

    It’s HD so you can see the code up close. Thanks to Mario for sending me a sample.

  • Joseph Nov 30th, 2010

    Could you please take a look at my code and tell me why this is not working? The code above is what we changed it to watching your video. Form below is the code that came with the template.

    <input type="text" value="” name=”q” class=”searchfield” onfocus=”if (this.value == ”) {this.value = ”;}” onblur=”if (this.value == ”) {this.value = ”;}” />

    <input type="image" class="submit btn" name="sa" src="/search-btn.png” alt=”Go” />

    ***** Form that came with template ********

    <form method="get" class="searchform" action="/”>

    <input type="text" value="” name=”s” class=”searchfield” onfocus=”if (this.value == ”) {this.value = ”;}” onblur=”if (this.value == ”) {this.value = ”;}” />

    <input type="image" class="submit btn" name="submit" src="/search-btn.png” alt=”Go” />

  • admin Dec 3rd, 2010

    Joseph,

    Based on the fragments you’ve posted, I can’t tell what it’s not working. The new code you posted doesn’t include the opening

    statement, but I’m not sure that was omitted because you didn’t include it or because you changed it.

    Anyways, I emailed you. If you send me the entire form codes (both new and old) I’ll take a peak.

  • Gina Nov 6th, 2011

    Hello, I am trying to use Google Custom Search for my WordPress site (Twenty Eleven theme) and basically trying to do what you show above, however, I think the most recent Google code has changed and no longer agrees with this tutorial (for a novice user). What I am trying to do is place the search element in the side bar of my WordPress template (embedded in sidebar.php code); no problem there. But I want the CSE to redirect the search results to a results-specific page (in the same way that WordPress displays search results (search.php)). I’m trying to avoid Google’s solution in which the content is simply pushed downward and out of the way to make room for the search results. Thank you for any help you can provide.

  • admin Nov 6th, 2011

    Gina – First, as far as where the search results open up, that’s a setting in your AdSense account. You can choose to have them open in your site on the page you specify or you can choose to have them open on a Google-owned page (that’s my preference). This article has more info about that: http://sporkmarketing.com/blog/598/google-adsense-search-review/

    Second, I’ve updated the information to match the Twenty Eleven theme. It should be much more useful for you now.

  • Gina Nov 8th, 2011

    Thank you for taking the time to make these changes. After looking at your other blog post, I learned that I was using Google code from their /cse page rather than their /adsense page. The code that Google provides is different depending on which site you choose to make your custom search engine. I guess the problem that I’m having is that I do not know how to make the CSE search results display on my site in the same way that WordPress displays their results (rather than using the Google hosted option or as what I described above in which the Google results push content aside). In the meantime, I’m still at it with trial and error.

  • Jose Nov 25th, 2011

    The code works. But I think this alteration of Google’s AdSense for Search code is against Google’s TOS. We can’t add a value for search input field. Actually we can’t make any change in the code which violates their policies.

  • admin Nov 25th, 2011

    Jose – I must confess that I almost never read any TOS agreement, and it makes sense that any alteration would be an issue.

    However, I don’t think the AdSense compliance team would find a problem with these modifications, provided the Google javascript (which places a watermark in the search field) is intact.

    Ultimately, modifying some extraneous HTML and css code in the snippet to make the form look like it belongs is a positive for the user.

Trackbacks and Pingbacks

    Leave a Comment