Insert Adsense Ads Without a Plugin

Updated on:

I have been using Ad Inserter to place adsense for a while because I think it is better than its competitors.

Having said that I enjoy having few plugins, lightweight plugins and plugins with only the options that I really need.

If you check Ad Inserter carefully, you will probably discover that you might not use many of its functionalities.

When I came to that realization, I started looking for code snippets to replace the ad inserter plugin.

These are some of the best codes to insert all types of ads without using a plugin

Where to Insert the Ads

I was looking for code snippets that can help me add adsense ads after paragraphs and after headings.

I figured that if you find one able to insert ads after paragraph, I could modify a tiny thing and use it to show ads after headings.

Anchor ads can be easily added with the help of the elements sections from GeneratePress.

Sidebars can be added using a HTML widget.

How to Insert the Code Snippet

Code snippets plugins have never been so popular as now so finding one to do this job isn’t that difficult.

I am using the free version of code snippets in some sites and WPCode in others.

Inserting the ad units code inside the code snippet that I am gonna end up using makes changing and adapting things a bit more complicated.

So shortcodes are the way to go.

Code Snippet to Insert Adsense Ads (#1)

There are many code snippets out there to insert ads, since I am not a coder, I can pick one and try to alter the code.

I tried this one first from CodexWorld and it works as it says it does

/*
 * Insert ads code into post content
 * $adsCode: Specify code that wants to add
 * $insertAfter: Specify paragraph number
 */
add_filter('the_content', 'cw_insert_post_ads');
function cw_insert_post_ads($content){
    if(is_single()){
        //ads code
        $adsCode = '<div>Insert your ads code here</div>';
        
        //insert after
        $insertAfter = 2;
        
        $closingP = '</p>';
        $contentBlock = explode($closingP, $content);
        foreach($contentBlock as $key => $con){
            if(trim($con)) {
                $contentBlock[$key] .= $closingP;
            }
            if(($key + 1) == $insertAfter){ 
                $contentBlock[$key] .= $adsCode;
            }
        }
        $content = implode('', $contentBlock);
    }
    return $content;    
}

This works for one ad so there are two things that you could do, use the code more than once but make modifications to it so it doesn’t create any conflicts so the second time you use this code, the code should look like this:

/*
 * Insert ads code into post content
 * $adsCode: Specify code that wants to add
 * $insertAfter: Specify paragraph number
 */
add_filter('the_content', 'cw_insert_post_ads1');
function cw_insert_post_ads1($content){
    if(is_single()){
        //ads code
        $adsCode = '<div>Insert your ads code here</div>';
        
        //insert after
        $insertAfter = 2;
        
        $closingP = '</p>';
        $contentBlock = explode($closingP, $content);
        foreach($contentBlock as $key => $con){
            if(trim($con)) {
                $contentBlock[$key] .= $closingP;
            }
            if(($key + 1) == $insertAfter){ 
                $contentBlock[$key] .= $adsCode;
            }
        }
        $content = implode('', $contentBlock);
    }
    return $content;    
}

I think that there must be a way to insert more ad units using this code but I don’t know how to do that.

If you want to insert ads after heading, use </h2>, </h3> or </h4>instead of ‘</p> in line #15

The problem that I found with my implementation is that code is gonna insert the advertisement where you want to but if you don’t have headings or paragraphs, all advertisements will be inserted at the end of the post.

That’s not a problem though.

I noticed the problem because I checked a post with only two paragraphs or something.

Code Snippet to Insert Adsense Ads (#2)

I found this code in StackOverFlow and it does what you need and you can easily modify it to add more ad units.

function prefix_insert_after_paragraph2( $ads, $content ) {
    if ( ! is_array( $ads ) ) {
        return $content;
    }

    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );

    foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        $n = $index + 1;
        if ( isset( $ads[ $n ] ) ) {
            $paragraphs[$index] .= $ads[ $n ];
        }
    }

    return implode( '', $paragraphs );
}

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
    if ( is_single() && ! is_admin() ) {
        $content = prefix_insert_after_paragraph2( array(
            // The format is: '{PARAGRAPH_NUMBER}' => 'AD_CODE',
            '1' => '<div>Ad code after FIRST paragraph goes here</div>',
            '2' => '<div>Ad code after SECOND paragraph goes here</div>',
        ), $content );
    }

    return $content;
}

You just have to duplicate line 28 or 29 and change the value.

If you want to insert ads after headings like I do, use </h2> or </h2> instead of </p> in line #6

If you use shortcodes, the code still look pretty after inserting several ads.

Plugins to Insert Ads Manually

For some reason, this code snippet added an additional ad units at the end of the post when there were no headings or paragraphs.

Code Snippet to Insert Adsense Ads (#3)

This code taken from Stack OverFlow also did the trick.

As code snippet #2, you can easily modify it to add several ad units

add_filter( 'the_content', 'add_ads_to_content' );

function add_ads_to_content( $content ) {

    $ads = array(
        2 =>  'ad code 1', // paragraph_id => ad_code
        4 => 'ad code 2', // paragraph_id => ad_code
        6 => 'ad code 3' // paragraph_id => ad_code
    );

    if ( is_single() && ! is_admin() ) {
        foreach ($ads as $paragraph_id => $ad_code) {
            $content = prefix_insert_after_paragraph( $ad_code, $paragraph_id, $content );
        }
    }

    return $content;
}

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }
        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
    return implode( '', $paragraphs );
}

This one didn’t cause any issue when the number of paragraphs or headings aren’t present in the post.

I guess I am gonna stick with this one.

ShortCoder

I like using shortcoder because it is a plugin that can help you do lots of things

If I want to modify the adsense code, Shortcoder is where I’ll do that.

This is how everything looks like so far.

ShortCoder - Insert ads without a plugin

WPCode – Insert Headers, Footers, and Code Snippets

In case you don’t want to install an extra plugin, there is another code snippets plugin in town.

The Ultimate Managed Hosting Platform

It is called codebox and you can create shortcodes with it.

CodeBox

You can manage the shortcodes and the code snippets to insert ads using only one plugin.

The Ultimate Managed Hosting Platform

JM

Sobre Jose Manuel

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find this blog useful.