Advertisement

How To Automatically Add Adsense Ads In the Middle Of Blog Post


One of the widely accepted and effective strategy of increasing your revenue from adsense display ad is to experiment with ads placement in different positions of page. One such location is middle of the post, which may increase the chances of noticing your ads and user engagement.

There are two methods of adding display ads in middle of any post in WordPress site.

1.Adding Adsense Code using plugin

This method requires small coding but donít be afraid as you can do it easily even if you donít know anything about PHP or coding. There are two ways adding code to middle of your post, you can choose anyone of it Ė

a) By editing Single.php

  • Login to your WordPress dashboard
  • Goto Appearance (at left sidebar) -> Editor
  • Open single.php file (at right sidebar)
  • Replace < ?php the_content(); ?> with following code
&lt; ?php
$content = apply_filters('the_content', $post-&gt;post_content);
$content = explode (' ', $content);
$halfway_mark = ceil(count($content) / 2);
$first_half_content = implode(' ', array_slice($content, 0, $halfway_mark));
$second_half_content = implode(' ', array_slice($content, $halfway_mark));
echo $first_half_content.'...'; echo 'YOUR ADSENSE CODE OR OTHER ADDS CODE LINK HERE';
      echo $second_half_content;
?&gt;
  • Replace YOUR ADSENSE CODE OR OTHER ADDS CODE LINK HERE with your own Adsense or any ads code.
  • Save and check your blog post to see if it works.

b) By editing functions.php

  • Login to your WordPress dashboard
  • Goto Appearance (at left sidebar) -> Editor
  • Open functions.php file (at right sidebar)
  • Copy and paste following codes in functions.php
function inject_ad_text_after_n_chars($content) {
  // only do this if post is longer than 1000 characters
  $enable_length = 1000;
  // insert after the first  after 500 characters
  $after_character = 500;
  if (is_single() &amp;&amp; strlen($content) &gt; $enable_length) {
    $before_content = substr($content, 0, $after_character);
    $after_content = substr($content, $after_character);
    $after_content = explode('', $after_content);
    $text = '
     <!-- YOUR ADSENSE CODE OR OTHER ADDS CODE LINK HERE -->
   ';
    array_splice($after_content, 1, 0, $text);
    $after_content = implode('', $after_content);
    return $before_content . $after_content;
  }
  else {
    return $content;
  }
}
add_filter('the_content', 'inject_ad_text_after_n_chars');
  • After adding the above code replace <!YOUR ADSENSE CODE OR OTHER ADDS CODE LINK HERE Ė> with your own Adsense or any ads code.
  • Save and check your blog post to see if it works.

2.Adding Adsense code with the help of plugin
You can also add Adsense display ads by using free WordPress plugin. There are many ad manager plugins. You can try any one. Here are some that I found useful and easy to use.