AdSense for WordPress – A Custom Function Snippet

I just finished writing about function snippets where I left out my AdSense function snippet for a good reason. It’s long. I don’t expect you to use this snippet as is. I have it set up perfectly for this website, with three AdSense ads within the content. If you want to put ads somewhere else, you’ll have to figure out how to do that yourself. I use 300×250 ads because I know it works on all platforms except the smallest mobile phones (at 250 pixel widths).

My AdSense Function Snippet

WordPress - adsense The best way for you to understand how it works is to see it in action. The code below is only slightly altered from what I’m actually using on this website.

Mostly it’s the added comments, but I also removed my AdSense code to keep it from showing up on other websites:

function insert_adsense_single( $text ) {
  if ( !is_single() ) return $text;

/** Top AdSense Ad */

  $adsense = '
<div style="float:right;margin:0 0 0 5px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- RTCX Post Top 300 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="0951650104"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<!-- google_ad_section_start -->
';
  if ( is_user_logged_in() ) {
    $adsense = '<div style="float:right;background:#ccc;border:1px solid #000;width:300px;height:250px;margin:0 0 0 5px;"> </div>';
  }
  $text = $adsense . $text;

/** AdSense ad between paragraphs */

  $adsense = '
<center>
<div style="margin:0;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- RTCX Paragraph 300 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="2070817607"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</center>
';
  if ( is_user_logged_in() ) {
    $adsense = '<center><div style="background:#ccc;border:1px solid #000;width:320px;height:300px;margin:0;"> </div></center>';
  }
  if ( strpos( $text, '<!--adsense-paragraph-->' ) ) {
    $text = str_replace( '<!--adsense-paragraph-->', $adsense, $text );
  } else {
    $array = explode( ' ', $text );

/** adjust the -3 for automatic paragraph insertion */

    array_splice( $array, -3, 0, $adsense . '
' );
    $text = implode( '', $array );
  }

/** Bottom AdSense ad */

  $adsense = '
<!-- google_ad_section_end -->
<center>
<div style="margin:15px 0 0 0;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- RTCX Post Bottom 300 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="7582966789"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</center>
';
  if ( is_user_logged_in() ) {
     $adsense = '<center><div style="background:#ccc;border:1px solid #000;width:300px;height:250px;margin:0;"> </div></center>';
  }
  $text .= $adsense;
  return $text;
}
add_filter( 'the_content', 'insert_adsense_single', 100 );

Some Explanations

I could comment the code more than I already have and make it bigger than it needs to be. I’ll explain what you need to know so I won’t have to.

This code includes AdSense section targeting. It also includes alternate ad displays for those who are logged in. There’s no reason to cause useless AdSense impressions.

HTML and CSS purists will probably hate the code. I use the “center” HTML tag, which is valid in HTML5. I use inline CSS.

The AdSense ad that appears within the content, between paragraphs, can be automatic or it can be adjusted. I use this within the content to control where it displays if I don’t like where it displays automatically.

<!--adsense-paragraph-->

What about WordPress Shortcodes?

Yes, you could do it with WordPress shortcodes. That is until you decide you don’t want to run AdSense ads anymore. It would be an incredible pain to remove all instances of those shortcodes from more than just a few posts.

If you decide to take that route, I suggest you become familiar with the Search Regex WordPress plugin. It’s what I use when I need to find or find and replace specific pieces of text.

Share this: