Automatic inclusion of Google Adsense ads in WordPress content.
Creating great content is one of the aims of publishing over the web using platforms such as WordPress. Trying to monetize a blog is another aim and advertisement inclusion is always a problem especially when talking about that of Google Adsense. On this very blog, you found out a tutorial on the easy way of including Google Adsense ads with the use of shortcode in WordPress as well as how to improve the quality of your Google Adsense ads and a guest post by Kurt from Icy Tips on the most performing Google Adsense ads size to use.
Publishing Google Adsense ads in single posts content only.
The use of shortcode is easy for such inclusion and quite handy as it comes through. It actually was the way I used to include my Google Adsense ads in my code. Here’s a newbie tip if you’re going to use the shortcode in your posts. Let’s say that you have decided to publish Google Adsense ads in your posts only. You can use your PHP-fu along with your WordPress-fu to generate your Google Adsense blocks in your posts only. This is how it works out. Make sure you’ve implemented your code as described in the post here: easy way of including Google Adsense ads with the use of shortcode in WordPress.
To make sure you publish only when you’re in a single post you need to write this code in your post:
if(is_single()) {echo "[adsense]";}
What we are doing here is basically asking the code to check if the current page is a single WordPress post page and publish the shortcode if this is the case. Easy as ever. I usually used this to publish 3 Google Adsense slots (maximum Google Adsense slots you can publish on a page) on my single page posts. When the post was published on the blog’s front page, no Google Adsense ad was visible.
Look Ma! No code!
There’s a way to go one step further! The idea is to publish your WordPress single post Google Adsense ad in your content without having to write anycode while publishing. You might not see why this might be handy but believe me, when you find yourself cleaning up hundreds of posts shortcode inclusion, you quickly try to find another way of doing it (yes, I did clean hundreds of posts manually removing Google Adsense ads in my content). There are 2 ways of doing this.
The first one which I would call the brutal way (maybe slightly more efficient for the non PHP experienced) which calls for direct inclusion of the Google Adsense tag in the code of the single post page, single.php file in the theme folder. You just need to add your Google Adsense slot before or after your content (the the_content() tag) as it pleases you:
The classy way of adding Google Adsense slots.
Now the classy way of doing things. It is the way the Google Adsense slots are set on the Web Design Bureau of Mauritius. I wanted to do this just to be able to style the Google Adsense slot right beside the content (the Google Adsense block had to be put right into the content and I did not want to have to put shortcodes all the time). So I had to put it directly into the content. After some research on the code I found out that I could write my own function to modify the code. Basically, I wanted to take all the content sent out by WordPress for my posts and inject my Google Adsense ad into it. Simple. So I had to create a dummy content that could be easily modified. This function must be put in the functions.php file of the WordPress template. Please not that what we are adding here is a filter.
function like_content($content) {
global $post;
$original = $content;
$content = "<div class=\"pub\">";
$content .= "<script type=\"text/javascript\"><!--
google_ad_client = \"pub-XXXXXXXXXXXXXX\";
google_ad_slot = \"XXXXXXXXXX\";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>
";
$content .= "</div>";
$content .= $original;
$content .= "</div>";
return $content;
}
add_filter( 'the_content', 'like_content' );
There you go, your Google Adsense slot will always be included directly into your content without having anything to implement. The only thing you will now have to do is to style your content through your CSS file.
One step further: Google Adsense in single posts only.
Now, let’s go even further in this. The idea was to generate a Google Adsense ad slot in the single post content. The function above has the enormous drawback of setting the Google Adsense block in ALL the content. By all, I mean all, i.e. pages and single posts. This was not the main idea was it? So we need to remove the filter from all the “pages” content. For this to happen, we need another filter function which will remove the filter if we are in a page. It looks like this:
function remove_like_content() {
if (is_page()) {
remove_filter('the_content','like_content');
}
}
add_action('wp','remove_like_content');
As you can see, the code analyses if the content is in a page with the is_page() method and then removes the filter from the WordPress loop by adding this Google Adsense slot suppresion.
Let’s talk about this.
I know that this is a bit of a creative way of doing things but the whole concept here is to get the best out of both worlds, Google Adsense‘s best performing ads with WordPress’ flexibility. Do you have any other creative way of including Google Adsense slots in your code? If so, please share.
Category: Making money




![Recommend [webdesign]](http://s3.amazonaws.com/arkayne-media/img/badge/logo-recommend-badge-medium.png)



This is awesome. I have really struggled with the wordpress vs. blogger fight. WordPress is definately better, and if this works, will win hands down.Thanks again for the great article!
Even if they’re both blogging platforms, WordPress might have different uses than blogger due to the CMS side of WordPress. Please leave a feedback if you succeed in implementing the code on your blog.
Thanks for the information. I always searching for this but I always have trouble with codes
any easy plugin bro?
Thanks.
Thanks for commenting Latief. I know that this code can really be useful but it comes from my different research over the web and personal experimentation. I’m afraid there’s no plugin doing this for the time being.
You can try Whydowork Adsense plugin. But still, I prefer this direct approach. A plugin less.. is always better
.-= Kurt Avish recently wrote: Install WordPress Securely For Beginners =-.
I never saw things this way. True that a direct approach means less resource wastage with a plugin. Thanks for the info on Whydowork Adsense.
.-= Web Design Mauritius recently wrote: Round up of the best posts of April 2010 on the Bureau. =-.
Interesting. I used my code my directly by putting it in the single.php file – However I always used a plugin when I wanted to right align or add it to specific page only. Nice tut. Gonna recheck it and implement after my exams over..Ahh exams
.-= Kurt Avish recently wrote: Mauritius Lotto Result For 8th May 2010 =-.
Good luck for your exams.
Yes, the advantage of this type of inclusion is that you have full control of your content and can implement all the CSS you want to it.
.-= Web Design Mauritius recently wrote: Round up of the best posts of April 2010 on the Bureau. =-.
Once again Sachin, a brilliant and informative article which will come in handy. Just a question for you? Are there really any viable adsense alternatives that can come anywhere near to the potential of earnings of adsense. I know Yahoo publisher is almost impossible to join. I’ve tried a few but they do not seem as intelligent as google and they are often too spammy.
Any ideas will be much appreciated.
Thanks in advance
Hi Eddy,
Glad that this can help you. As for ad tools I must say that I’m not an expert but did try working a bit on this. Adsense is the best paying one provided you keep a high CTR (Click Through Rate). I’m trying Infolinks now and it seems to be working. As a matter of fact I’ve never been paid by Google over the past 6 years (not enough traffic).
.-= Web Design Mauritius recently wrote: Custom Search: make the most out of Google Maps with optimized geolocation results =-.