How to insert Adsense Code in the Middle of a Post?

by ishuin
9 replies
  • SEO
  • |
Hello all,

Does anyone know of a way to insert Adsense code in the middle of a long Wordpress post? Assuming posts are usually >450 words and are of variable length.

There's already an Adsense placement at the top and bottom of each post. I thought it'll be good to add one in the middle as well.

Tried adding 160X600 at the sidebar but clickthrough is pathetically low.

Thanks for any advice.
#adsense #code #insert #middle #post
  • Profile picture of the author theconsultantseo
    which CMS are you using. Wordpress?
    {{ DiscussionBoard.errors[2902677].message }}
    • Profile picture of the author ishuin
      So sorry for the delayed response. I didn't know there was a response to my question.

      The answer is yes, I'm using Wordpress.

      Actually, I've found a script on the web to do it. But, eventually I didn't use it because due to the nature of my site, inserting an ad automatically in the middle of a page will ruin some of my pages.

      I'm reproducing the PHP script here for those who may need a similar script. The script is not from me, so I cannot take credit for it, neither can I provide any support.

      //Find an appropriate position near the middle of the content to insert an ad
      function insert_ad_in_middle_post($content) {
      $output = '';
      $positions = array();
      $endpos = -1;
      $trigger = "<p";
      if(strpos($content, "<p") === false)
      $trigger = "<br";

      while(strpos($content, $trigger, $endpos+1) !== false){
      $endpos = strpos($content, $trigger, $endpos+1);
      $positions[] = $endpos;
      }
      $middle = sizeof($positions);
      while(sizeof($positions) > $middle)
      array_pop($positions);
      $triggerpoint = $positions[floor(sizeof($positions)/2)];

      if ($middle > 2) {
      $ad_style = 'clear:both;margin:15px 0 15px 0;padding:0;border:1px solid #ccc;';
      if(is_single()) {
      $content = substr_replace($content, '<div style="'.$ad_style.'"><!-- Insert Ad Code --></div>'.$trigger, $triggerpoint, 2);
      }
      }

      $content = $output.$content."<div style='clear:both'></div>";
      return $content;
      }
      add_filter('the_content', 'insert_ad_in_middle_post');
      {{ DiscussionBoard.errors[3452042].message }}
      • Profile picture of the author donkey007
        right now i m using two types of ad manager plugin for wordpress.

        one is advertising manager which allows me to put ad code in sidebar widget and other plugin ( i dont remember the name now) to insert ad code between top , bottom , before comment box . i dont know it allow to put in the middle of post.

        i m using mystique theme which has advertising section in theme settings , which that you just need to insert a tab like [ad] between posts.
        {{ DiscussionBoard.errors[3452508].message }}
        • Profile picture of the author goendul
          @ishuin: where you put those scripts? function.php or single.php, thanks
          {{ DiscussionBoard.errors[6869687].message }}
  • Profile picture of the author brainfreeze0
    here's the simplist way that I use. In HTML mode at the top of the page/ post your writing:

    <center>adsense script</center>

    If you want it actually within the editor code go to page.php, index.php, post.php and look for the <div=main role=content> and cant remember but its either just above or below that tag
    {{ DiscussionBoard.errors[6869752].message }}
    • Profile picture of the author saganator
      Just switch to HTML view and insert the script into the post.
      {{ DiscussionBoard.errors[6870034].message }}
  • Profile picture of the author neo087
    Originally Posted by ishuin View Post

    Hello all,

    Does anyone know of a way to insert Adsense code in the middle of a long Wordpress post? Assuming posts are usually >450 words and are of variable length.

    There's already an Adsense placement at the top and bottom of each post. I thought it'll be good to add one in the middle as well.

    Tried adding 160X600 at the sidebar but clickthrough is pathetically low.

    Thanks for any advice.
    Hi, You can insert adsense Code in the middle of any WordPress post by using "Quick Adsense" Plugin. The plugin is really awesome and you can insert Adsense code any where of your posts. Just try it. I am sure it will be helpful for you.
    {{ DiscussionBoard.errors[6871825].message }}
  • Profile picture of the author tylerjaysen
    yeah it's really easy..just insert your adsense code into the html section and hit save. Blam done.
    {{ DiscussionBoard.errors[6872363].message }}
  • Profile picture of the author worrall
    The better way is to switch to the html and then insert the Adsense Code into the post because html being the simple and you easily find the errors if they occur in the code.
    {{ DiscussionBoard.errors[6872455].message }}

Trending Topics