Ask me a question at the Wordpress Ninja Forum
Adding Google Adsense To A New Wordpress Theme
Posted on August 1, 2008
Filed Under Guides |
Ok, so you downloaded a new theme that totally rocks in every single way, except one thing - there’s no pre-setup Google Adsense code
That’s where the Wordpress Ninja comes in - cue…me
Before we do anything, you need a Privacy Policy to conform to Google’s TOS. This ‘Privacy Policy Plugin‘ is great - it automatically creates it for you after just a couple of options being set for it. Wordpress Ninja highly recommends this plugin.
There are a lot of decent Google Adsense plugins available, but i thought i would show you the hard way how to insert Adsense manually so you have full control over what you are doing. Once you know the long and hard way the plugins will look like they are for Newbies!
If you do want to use a plugin for managing Google Adsense code in your blog then i can recommend this one, though there are many you can choose from. Have a search and find one that best suits your needs.
Ok, back to the manual creation…
First step - which i’ll assume you have done already - is to setup your Google Adsense account. I’m not going to go into the account setup and Ad setup as there are many great tutorials and help files on the web for this - the Google help itself is pretty decent. Some important points to remember when setting up new ads are this:
- Make sure you color-code the ads to blend in with your site. Just do a Google search for ‘optimizing adsense’ and you’ll be able to find a lot of great articles on how to go about this
- Create channels so you can track your different ad blocks’ performance
- When you create a new ad block, copy and paste the code they give you into a text editor and keep it all handy. Neatly organize them all so you know which code belongs to which type of ad block e.g. 160×600 Skyscraper - then the code underneath.
- Think about how you want to organize your ads on your blog theme e.g. do you want a large leaderboard banner across your blog page? Will it even fit? Do you want ads in the sidebar? Do you want a square ad block in each of your posts (like the classic Prosense theme)? Create the ads you will need based on your theme design. Due to the size constraints of your theme you may have to do a bit of trial and error to get the right ad units for your site.
Ok, once all the ads are created and you have saved out your code blocks, open your favorite code editing app (i listed several good ones in this post) and start editing your Wordpress files. Now, depending on your desired ad placement, you need to modify the appropriate .php files within your wp-content/themes/mytheme/ folder. For example, if you want any ads in your sidebar then you need to edit sidebar.php. Remember my previous post about the way Wordpress themes are layed out in your site, broken into parts - Header, Content Body, Sidebar(s) and Footer. This is important because it helps you to work out which file you need to edit. Here’s a quick breakdown of the key files you will need to edit that sit under your theme folder:
- Sidebar.php is for the sidebar (well, duh?) - if the sidebar is divided into left and right sections then this file will usually hold both left and right sidebar sections. They will usually be setup as CSS divisions rather than seperate files. This is where you would usually add a skyscraper style Google ad unit
- Index.php is the main content - imagine it like a framework that loads the posts and everything in the main body. This is where you might add Link Ad Units at the top - depending on where your NavMenu (if any) is located. Basically anything added here will show up on all pages.
- Single.php is very important - this file is essentially each individual post, so you would add any ad units here such as ads inside each post, or directly above or below each post. The key point is that ads in here apply to EVERY post.
So you now know which file to edit. I’m going to use an example of a sidebar ad unit (a long skyscraper), so i start editing sidebar.php in my code editor. I want to place the ads in the left section of my sidebar under my pages list, so i find the code section that contains something like <div id=”left_sidebar”> or something like that. Then i find the code that contains “WP_list_pages” which is our page list - this is probably enclosed in <div> tags or <ul> or something similar. We want the Google Ad code to appear under the page list so we copy our Google Ad skyscraper code from the text editor we saved it to earlier and paste it into our sidebar.php file at this section. You dont have to enclose the Google Ad code in any tags, but i generally chuck inside a <div></div> tag just to keep it all looking nice.
That’s basically it. Save your changes and upload to the your server, overwriting the original (you did take a backup of your server file first, right? Always take a backup just in case you need to revert back).
Also remember that when you create a new Google ad unit it can take 15mins or so to become active - until this time it will be blank - dont freak out though - they should turn up shortly. Allow some time also once it is active before the ads become relevant. If they are still irrelevant after a day or so then check out my post on forcing relevant Google Adsense ads in your Wordpress Blog.
So i know this article has been rather generalized, but hopefully you got something out of it. As always feel free to ask me any questions.
Comments
7 Responses to “Adding Google Adsense To A New Wordpress Theme”
Leave a Reply
Related Posts:
Hi, i am the Wordpress Ninja. My name is Paul and my goal is to be able to answer everyone's Wordpress questions (for free) and post the best solutions on this blog. So for all questions and issues please post in the
Thanks for the great how-to lesson.
One problem that I came across… Google says you can’t have more than 3 of any type of ad on a page. I thought that meant blog post, but apparently it means the whole page with up to 10 separate blogs on it.
And then, of course, the whole thing is searchable by topics, so how do I stay on top of this and make sure that no more than 3 of the same ad thingies appear on any one page???
Thanks a bunch.
Hi Elisabeth,
Google’s TOS basically says you are not allowed more than 3 of the same kind of ads per page. This means you can have 3 ad blocks and 3 ad link units on each page, but let’s just focus on the ad blocks for now. A good model to follow would be that of the Prosense theme which uses it’s ad blocks very wisely.
Lets assume you have a skyscraper ad block in your sidebar - that’s 1 down permanently as it shows on every page (being in the sidebar). This leaves you 2 blocks to play with. If you have a look at my http://www.broadwaymusicals.asia blog you will see i have an ad block in the sidebar and also an ad block when you click on each blog post. The main home page (that shows 10 blog posts) does not show an ad block for each post though (which would be 10 ad blocks + the 1 sidebar block = bad).
If you put the adsense code to the single.php file then it will only show the ad when you click on that single post and NOT on the main homepage that shows the 10 blog posts or whatever. If you put the adsense code into index.php after the post section then it will show up after each post on the main home page - resulting in 10 ad blocks for example, which is bad.
So, if that all didn’t confuse you, then to summarize - think how Wordpress loops your posts. The index.php file is the one that could loop your ad blocks, so that is very important. Be careful where you put the code in there - make sure it is not inside the loop or it will be repeated for each post.
To get it looking similar to Prosense, you would add the Adsense code as follows:
- sidebar.php - under the left sidebar section
- index.php - at the top above the post section
* Prosense does a really cool thing inside the while loop which lists all of the posts - they make it add an ad block only after the second post (this is to get near the fold), so as the loop goes through it keeps count of the posts and says if the count is 2 (i.e. the second post is completed) then show the ad block. Then when the count goes up to 3 or more it does not show it. This keeps the total ad blocks for the page to 3 (sidebar, top banner and near the fold). If you want to do something like this let me know and i’ll show you the code.
- single.php - just under the ‘post’ but above the ‘entrytext’ section
Also, curious why you need an opt-in form for the Privacy Policy? That plugin covers everything required by Google TOS, so you don’t need any opt-in form…
Hope this helps.
regards,
Paul.
This worked like a charm for me. I just transitioned from Wordpress.com to my own site and wanted to add Adsense on the sidebar. I put the adsense code between two tags at the bottom, added a to give it some nice spacing and it was up and running in no time.
Thanks
Todd
http://blog.penguintrail.com
thanks for your sharing
[...] post: Adding Google Adsense To A New Wordpress Theme | Wordpress Ninja Comments0 Leave a Reply Click here to cancel [...]
i dont know why my link unit ads only appear once. any ideas? the maximum should be 3 right? i placed the code just before the end while code but only 1 shows up
Exactly what i was looking for,thanks for sharing this?