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.








