Lookie: a video from Inside AdSense!
I went through a long list of difficult trial and error the other day, trying to get the new AdSense for Search results working in WordPress. After creating a static page, I posted this in another static page, and tried to get it to work.
Obviously, now it works, but I’d tried a different template, doing a lot of hacks, then thought I had it all correct. Then, I got an email from the AdSense team asking what I couldn’t make work. In an effort to show how it didn’t work, I found out that I was over-thinking the process.
After creating the static page, WordPress adds breaks (the BR tag) to every line. While I was hammering away at trying to get that working, it dawned on me that I was working too hard at this, and I should’ve done the simplest thing first: created a template with the javascript code in it. Seeing the automatic line breaks in the source code of the page made me realize I needed to stick the javascript Google provides in the source of the page, not the story.
I created this template, which you’ll get if you search in the box above. It’s reproduced here for your copy and pasting and stealing without attribution needs:
<?php
/*
Template Name: Search Landing
*/
?>
<?php get_header(); ?>
<div class="content">
<div class="primary">
<h2>Search Results</h2>
<p><!-- Google Search Result Snippet Begins -->
<div id="googleSearchUnitIframe"></div>
<script type="text/javascript">
var googleSearchIframeName = 'googleSearchUnitIframe';
var googleSearchFrameWidth = 650;
var googleSearchFrameHeight = 1300;
var googleSearchFrameborder = 0 ;
</script>
<script type="text/javascript"
src="http://www.google.com/afsonline/show_afs_search.js">
</script>
<!-- Google Search Result Snippet Ends --></p>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
To use AdSense for Search on your site, follow these steps:
1. Most importunely, you need a Google AdSense account, and Google hands them out like candy, so don’t worry about getting rejected. I’d put an affiliate link in here, but Google doesn’t let me. DARN YOU GOOGLE AND YOUR DARNABLE MULTI-LINE JAVASCRIPTS!!!
(Note: this is the desired behavior of WordPress and AdSense, that was just a little joke. Please don’t cancel my account!)
2. Upload the template you just created to your template folder, and name it something like searchresults.php, it doesn’t really matter. Just don’t name it search.php, as that will overwrite the built-in WordPress search file.
3. Create a new static page you want the search results on. You’ll need the name of this page when you create the AdSense for Search code, so make it memorable. Since it really isn’t important to the overall operation of your site, don’t name the page “Search.” I’m not saying that it’ll mess up anything, but it might conflict with the built-in search function, depending on your server. You can see what I’ve named my search results page.
4. Change the page template you uploaded to “Search Landing,” or whatever you named it if you’re being a maverick. On the right side of the page management screen, you’ll see the “Page Tempate” box, and you change the drop-down menu to “Search Landing.” If the template you uploaded doesn’t show up in the list, then check to see if copied it correctly.

5. If you haven’t already, get the page you created and enter it in the AdSense setup.
6. Insert the search code in your sidebar template, wherever you want the search form. Test it to make sure everything’s working correctly, then sit back and wait for the filthy lucre that comes with Google ads! Burn some hundred-dollar bills in anticipation.
That’s it, and your results should show up as normal. Next time, no thinking for me!
Which, of course, would be normal.
Comments? Questions? Ask me here.
Look at the former frontpage image by clicking here. The original story with flawed tech is on page 2.
Pages: 1 2
0 Responses to “A primer on getting the Google AdSense for Search into WordPress”