How to Manually Add AdSense in a WordPress Post

  • Buffer
  • Pin It
  • Sharebar
  • Buffer
  • Pin It

You can manually add AdSense in a WordPress post above or below the content without any issue. You can simply place the necessary code above, or below, the the_content(); function call. Now if you want to place it within the content, it is a bit more complex.

Most people would install a plugin that will automatically insert your AdSense code into the content of any post. I have used them in the past, but found that it would break the post readability if the code was inserted into the wrong location. Also, some people wish to reduce the number of plugins that are activated in the blog, so a manual method may be better. Once the code is added manually, you can easily monitor your AdSense coverage.

Manually Add AdSense in a WordPress Post Without a Plugin

google adsense logo 256x256 How to Manually Add AdSense in a Wordpress Post

In order to place AdSense in your post without a plugin, you would need to edit the single.php file in your theme. You will then replace the the_content(); function call with the code below.

Code to Manually Add AdSense in a WordPress Post

<?php
$show_after_p = 1;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $show_after_p)
{
  $contents = explode("</p>", $content);
  $p_count = 1;
  foreach($contents as $content)
  {
    echo $content;

    if($p_count == $show_after_p)
    {
    ?>
    <!-- Insert code here -->
    <?php
    }
    echo "</p>";
    $p_count++;
  }
}
?>

I should note, however, that the code isn’t mine because I found it online (if someone knows the source, please let me know so I can give proper credit). Basically, you would insert your AdSense code by replacing this line:

<!-- Insert code here -->

After doing that, you can then choose to display the AdSense ads after a specific paragraph by indicating a paragraph number in the following line:

$show_after_p = 1;

While this code only allows you to add AdSense after one paragraph, if you know PHP, then you can modify the code to allow multiple AdSense code insertions.

The above code allows you to manually add AdSense in a WordPress post content so you don’t need to use an additional plugin.

You may also like:

No thumbnail image

How to Easily Add Adsense to Your Blogger Blog

Wordpress Logo

Creating Printable Post Pages in WordPress

Google AdSense Logo

How to Determine Your AdSense Coverage

Wordpress Logo

Correcting ThickBox Path in WordPress

18 people had something to say about “How to Manually Add AdSense in a WordPress Post”:

Comments


  1. Knowing how to do little php fixes like that – it’s gold! And if you dont’ use a plugin, it doesn’t slow down your blog either. Good stuff!
    Barbara Ling, Virtual Coach recently posted…Which lurking reader is helping YOUR blog score the perfect touchdownMy Profile


    • Until I started this blog I never programmed in PHP before. Performing tasks without the use of a plugin, and learning WordPress in general has helped me learn PHP at the same time.


  2. Thanks for the great tutorial. I am using another simple way to add Adsense code, that’s the Quick Adsense plugin. I only need to install the plugin, setup ad codes and then insert some codes like to anywhere in my posts.
    Tuan recently posted…The Stablest URL Shortener on the WebMy Profile


    • I have used such plugins, but I am not a fan of inserting codes in my posts. This is because I may want to change where they are located, and don’t want to go through all the posts to change the code placement.


  3. And it will showing your ads code after first paragraph.
    Android recently posted…Samsung Drops Galaxy Tab Prices In the UK But Still More Than AT&TMy Profile


  4. Nice tutorial.will try that


  5. This is a great tip, Paul. Anytime code can easily be used instead of a WP plugin is a big plus, IMO.
    Twitter:


    • I agree. I try to limit the number of plugins that I use, especially the ones that I can code manually into my template. There are several plugins that do come in handy, such as SEO and backup plugins.


  6. hmmm..interesting. i wonder if this would work on a ning blog..thanks


    • I’m not familiar with ning, but if it is PHP based it may work as most of the code is standard PHP. You would have to substitute any WordPress functions, though.


  7. For me I use a plugin called Quick Adsense. It is really easy to use without using and codings… You should try it out
    Kharim Tomlinson recently posted…How To Deal With Negative Listings In Search EnginesMy Profile


    • Thanks, I may look at Quick AdSense, but I find it easier to edit the template manually. I guess I am a hands-on type of guy.


  8. Hi Paul – it’s possible you got your inspiration from this article?
    http://www.dollarshower.com/how-to-blend-your-adsense-block-within-the-wordpress-post-content-conditionally-display-it/

    Either way, this is a great code snippet.
    Cheers!

    _Khalid
    Khalid J Hosein recently posted…Download Firefox to Treat Red PandasMy Profile


    • The article had good instructions, but I don’t think it was that one. While the code is similar, I remember the article I read actually had text instead of images.


      • Gotchya.

        By the way, do you know if your code works when the article is paged, i.e. when you use the WordPress operator to break it out into multiple pages?
        Thanks!


  9. i think it would work if in your post you assign …. in every single post.. it would be unconfortable..
    I just think using simple code plugin.. U can found at http://www.aming.info/increase-the-google-page-ctr-by-adsense-rotator-in-php/
    http://www.aming.info/adsense-injection-plugin-in-single-post/
    http://www.aming.info/smowtion-ensemble-of-ads-wordpress-plugin/


  10. tips of use indeed for those who are not that fluent with wordpress code customisations.
    Chandan recently posted…Problem with earning money online without investment in IndiaMy Profile


  11. Thanks man,

    i’ll use it for my blog.

Do you have something to say? Let everyone know!

Commenting policy: All comments are moderated for spam. You must use your real name and not your website name or keywords. If a comment is deemed to be spam, then it will be deleted or edited. Links to your website within the comment body is not permitted, but you are free to use CommentLuv to add a link to your latest post. If you wish to add a link to your website, you can always contact me about submitting a guest post.






CommentLuv badge
This blog uses premium CommentLuv which allows you to put your keywords with your name if you have had 5 approved comments. Use your real name and then @ your keywords (maximum of 3)

Previous Post:

Next Post: