WordPress - Coding tutorial

How to insert Google Adsense ads by word count in a post

In this WordPress tutorial you’ll learn how to insert Google Adsense ads by word count in a post without the need of using a plugin.

There are times when posts that you write on a specific topic has a good amount content and some other posts that have important information that you want to transmit, but they don’t have a great amount of content. Sometimes when posts are short, they lack of keywords, when this happens Google will struggle trying to figure out what is your content about making it difficult to target the right ads on these posts — when you are sharing wallpapers and photos, these are good example where there is going to be a lack of keywords and content.

Not only Google Adsense is going to have trouble with ads, but also your posts will look cluttered and people don’t like that — You have to try to give your visitors the best experience possible.

One other thing to keep in mind is that the more ads you have in a particular page/post, you are making it more difficult for visitors to pick what they want and you may end up not earning enough because of it.

Well, is there a solution for this? The answer is Yes, and the solution is pretty simple and it doesn’t require to install a plugin, just cutting and pasting some code into your WordPress template.

[stl]Inserting Google Adsense ad code by word count in a post or page[/stl]

To do this, you just need to add a simple function to the function.php and a simple hook in your WordPress template, and then ads will appear only when your new post contains a specify word count.

Follow these steps:

1. Open your function.php file with your favorite editor and inside the PHP code () add the following code:


/** Count Post Word count **/
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(" ", $content));
}

2. Save the function.php file and up load the file back to your web server if necessary.

3. Next, open the single.php file, locate where is that you want to insert the Google Adsense ad code by word count, and cut and paste the following code:


<?php if (wcount()> 300) {?>

/**Google Adsense goes here**/

<?php };?>

4. Don’t forget to add your Adsense ad code where it says “/**Google Adsense goes here**/”, then just save the file and upload it to your web server if necessary.

The above code in step #3 essential it says: if the word count of the post is grater than 300 words, then whatever is contained within the wcount block will be displayed, in this case a Google ad. But you are not just limited to ads, you could, for example, insert an image or any other block of code that you want. And if the post has less than 300 words, whatever is contained inside of the wcount block will not be display.

By doing this you are going to help Google to target ads to your audience more efficiently, potentially increase your earnings, and your posts are not going to look cluttered.

One thought on “How to insert Google Adsense ads by word count in a post

  1. Thanks!
    The steps provided here is very simple and useful to insert Google Adsense ad codes.

    Thanks again
    Regards

Leave a Reply