Insert Google Adsense Code Anywhere Randomly Within Your Wordpress Article

6 Comments September 8, 2009  |  by Philip Ze
Categories : Technology Internet

In general, placing the Adsense blocks randomly is one way to optimize Adsense. If you have many regular visitors who come back visit your blog frequently, this will likely catches their attention.

A plugin version of this code is now available.
Download from TechMilieu.com

In response to a comment by Gossips in my previous post Insert Google Adsense Code Anywhere In Your Wordpress Article, I have written a wordpress php function, which will randomly insert the Adsense codes anywhere within an article.

Take this page for example, two Adsense blocks have been selected randomly from four Adsense codes, display randomly within three positions.

Template Files Modification

The concept here is similar to the previous post, which involves adding some PHP & HTML code into your Wordpress theme. Once again, I will walk through the pieces of code to show you how this modification works. I have to assume you have the basic understanding on how to edit the Wordpress PHP files (your theme template). Backup your theme template before making any modification.

Modify functions.php

The method is to put HTML tags ( <!--AdsensePosition1-->, etc. ) anywhere within the post article, and call a PHP function in your template file to replace these tags randomly with your Adsense Codes.

The first step is to append the following PHP code to the end of your functions.php theme template file (Go to Appearance > Editor > Theme Functions on your Wordpress Administration Panel). Also, remember to replace the Adsense Codes with your own Adsense Codes, & modify the DIV CSS style. To add more Adsense codes, simply copy & paste the code block with the variable $RndAds[n], and continue with the numbering ‘n’.

function get_the_content_with_formatting ($more_link_text='(more)', $stripteaser=0, $more_file='')
{
   $content = get_the_content($more_link_text, $stripteaser, $more_file);
   $content = apply_filters('the_content', $content);
   $content = str_replace(']]>', ']]&gt;', $content);
   return $content;
}

function AddRandomGoogleAds($content)
{

  // -----------------------------------------
  // The example here display 2 Ads out of the 3 Ads codes ($RndAds[n]).
  // Modified this parameter for the number of Ads to be displayed.

  $No_Of_Ads_To_Display = 2;

  // ------------------------------------------

$RndAds[1] = <<<RNDDOC1
<div style="float:left;width:250px;height:250px">
<script type="text/javascript"><!--
google_ad_client = "pub-9999999999999999";
google_ad_slot = "0000000000";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
RNDDOC1;

$RndAds[2] = <<<RNDDOC2
<div style="clear:both;width:728px;height:90px">
<script type="text/javascript"><!--
google_ad_client = "pub-9999999999999999";
google_ad_slot = "0000000000";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
RNDDOC2;

$RndAds[3] = <<<RNDDOC3
<div style="float:left;width:336px;height:280px">
<script type="text/javascript"><!--
google_ad_client = "pub-9999999999999999";
google_ad_slot = "0000000000";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
RNDDOC3;

  for ($i=1; $i<=100 ; $i++) {
    if(strpos($content,'<!--AdsensePosition'.$i.'-->') == 0) { $AP=$i-1;break; };
  }
  if($AP==0){ return $content; };
  $DS = $No_Of_Ads_To_Display;

  for ($i=1; $i<=count($RndAds) ; $i++) { $TmpAds[$i]=''; };
  for ($i=1; $i<=$AP ; $i++) { $RepAds[$i]=''; };

  for ($i=1; $i<=$DS ; $i++) {

    do {
      $rm1 = rand(1,count($RndAds));
    } while ($TmpAds[$rm1]!='');
    $TmpAds[$rm1] = $RndAds[$rm1];

    do {
      $rm2 = rand(1,$AP);
    } while ($RepAds[$rm2]!='');
    $RepAds[$rm2] = $TmpAds[$rm1];

  }

  for ($i=1; $i<=$AP; $i++){
    $content = str_replace(array('<p><!--AdsensePosition'.$i.'--></p>', '<!--AdsensePosition'.$i.'-->'), $RepAds[$i], $content);
  }

  return $content;
}
 

NOTE : if you have added the functions which I posted previously (here), just remove the above get_the_content_with_formatting function to avoid duplication.

Modify single.php

Modify the line <?php the_content('(more)'); ?> (parameter may be difference) in your single.php theme template file, replace it with the following code:

<?php
   $cont = get_the_content_with_formatting();
   $cont = AddRandomGoogleAds($cont);
   echo $cont ;
?>
<?php /* the_content('(more)'); */ ?>

NOTE : if you have added the functions which I posted previously (here), just add $cont = AddRandomGoogleAds($cont) ; to the line after the $cont = AddRandomGoogleAds($cont); will do.

Insert Adsense HTML Tags to Your Post

Okay! That’s all you need to do with your theme template. Now, switch to the HTML tab in your Wordpress editing panel and insert <!--AdsensePosition1-->, <!--AdsensePosition2-->, etc. into your post article as many and anywhere as you wish, but limited to 100.

Now, view your article page and see your Adsense appear randomly at the desired location. Have fun! I am looking forward to your valuable feedback.


Comments

Say somthing about this post

Trackback

Those who link to this post

  1. Gossips says:

    Wow, that is really cool…

    Now, is this function can be a plugin? I mean, because if you are to use this function to work, you still need to modify your wordpress codes like functions.php. And you still need to paste a code into your post in order the function to work.

    Now if it is a plugin, then all you have to do is intall it as a plugin and no need to do the modifications on your .php’s and no pasting of codes into your edit html page for a post.

    Is this really possible to make it as a wordpress plugin? - This is what I am really looking for. Plugin that inserts adsense into your wordpress blog posts RANDOMLY.

    Hope you can make it, if ever, let me inform.

    Thanks.

    • Philip Ze says:

      I never have written wordpress plug in before, but would love to give it a try. Please stay with me, will inform you hopefully once it is ready.

  2. I am unsure if Adsense allows us to do marketing our website so we can increase the visitors. (i.e. put our advertise on other website)

Leave a Reply

Get a Gravatar if you like to show your picture with your comments.