Technology, life, programming, travel and money.

AdSense Injection WordPress plugin tweaks

Update 7th December 2010: Use the new Ad Injection plugin for WordPress instead as it will do what these tweaks do, and more.

On this blog I use the Adsense Injection plugin to automatically add Google’s targeted AdSense adverts to my posts. The plugin is excellent and has helped to make more money for this site but there are a few tweaks that I have done.

First, I’ve made a modification so that if a individual post page is shorter than a certain number of characters then the number of adverts is limited to one.

Second, I’ve made a change so instead of looking for ‘<p’ tags to use as AdSense insertion points it looks for the full paragraph tag – ‘<p>’. The reason I did this is because I often include code in my posts in <pre> tags and the AdSense plugin code was inserting adverts in my source code snippets which looked wrong. After making this change the adverts only appear in the text of my posts.

Both sets of changes are shown in the code below. You can simply replace the ai_the_content() function in version 2.0 of the plugin with this version.

adsense-injection.php
function ai_the_content($content){
  global $doing_rss;
  if(is_feed() || $doing_rss)
    return $content;
  if(strpos($content, "<!--noadsense-->") !== false) return $content;
  if(is_home() && get_option('ai_home') == "checked=on") return $content;
  if(is_page() && get_option('ai_page') == "checked=on") return $content;
  if(is_single() && get_option('ai_post') == "checked=on") return $content;
  if(is_category() && get_option('ai_cat') == "checked=on") return $content;
  if(is_archive() && get_option('ai_archive') == "checked=on") return $content;
  global $ai_adsused, $user_level;
  if(get_option('ai_betatest') == "yes" && $user_level < 8)
    return $content;
  if(get_option('ai_notme') == "yes" && $user_level > 8)
    return $content;
  # RML: backup content
  $original_content = $content;
  $numads = get_option('ai_nads');
  if(is_single())
    $numads = get_option('ai_nadspp');
  $content_hold = "";
  if(strpos($content, "<!--adsensestart-->") !== false){
    $content_hold = substr($content, 0, strpos($content, "<!--adsensestart-->"));
    $content = substr_replace($content, "", 0, strpos($content, "<!--adsensestart-->"));
  }
  while($ai_adsused < $numads)
  {
    $poses = array();
    $lastpos = -1;
    # RML: change <p to <p> to stop ads before <pre>
    $repchar = "<p>";
    if(strpos($content, "<p>") === false)
      $repchar = "<br";
    while(strpos($content, $repchar, $lastpos+1) !== false){
      $lastpos = strpos($content, $repchar, $lastpos+1);
      $poses[] = $lastpos;
    }
    //cut the doc in half so the ads don't go past the end of the article.  It could still happen, but what the hell
    $half = sizeof($poses);
    $adsperpost = $ai_adsused+1;
    if(!is_single())
      $half = sizeof($poses)/2;
    while(sizeof($poses) > $half)
      array_pop($poses);
    $pickme = $poses[rand(0, sizeof($poses)-1)];
    $replacewith = ai_pickalign(get_option('ai_lra'));
    $replacewith .= ai_genadcode()."</div>";
    # RML: remove hardcoded length
    $content = substr_replace($content, $replacewith.$repchar, $pickme, strlen($repchar));
    $ai_adsused++;
    # RML: if content is short then limit to one advert
    if(strlen($original_content) < 2500) return $content_hold.$content;
    if(!is_single())
      break;
  }
  return $content_hold.$content;
}



Reader Feedback

2 Responses to “AdSense Injection WordPress plugin tweaks”

  1. hank says:

    thx.

    Would be great to have some word count options in the settings.

    For instance no ad injection for less than 300 words, one for 300-600 words, two for 600-1200 ….

  2. AdSense Tricks says:

    Thank’s for the tips

Leave a Reply

Do NOT fill this !