HowTos

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

Written by Raj

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.

If you liked this article, then feel free to share it with your friends on Facebook because at RedSome we believe, sharing is caring.

Like this Article? Subscribe To FREE RedSome Updates!

About the author

Raj

2 Comments

  • Hello, I would like to add adsense code in a similar manner but a little apprehensive about editing the files as my knowledge is absolutely zero and I do not want to mess things up. Can you please help?

  • Hi,
    Thanks for your article. This was exactly what I was looking for. Going to implement your recommendations in my blog.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.