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

by 9 replies
12
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.
#search engine optimization #adsense #code #insert #middle #post
  • which CMS are you using. Wordpress?
    • [1] reply
    • 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');
      • [1] reply
  • 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
    • [1] reply
    • Just switch to HTML view and insert the script into the post.
  • 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.
  • yeah it's really easy..just insert your adsense code into the html section and hit save. Blam done.
  • 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.

Next Topics on Trending Feed