Please disable ad blocker to see the page.

Insert Ads Inside Your WordPress Post Content Automatically

However, you can insert Ads in the sidebar easily but in posts you have to do it manually and it takes much work. Here I would like to explain the ways to insert ads inside WordPress Post automatically.

This method needs two functions. First will find the place and second will insert the ads in that place. So let's take a look at the below codes.

function place_adv( $content ) {
 
    $ads = '<div>ADS code here</div>';

    if ( is_single() && ! is_admin() ) {

        return add_it( $ads, 2, $content );
      // 2 is the number of paragraph
    }
    
    return $content;
}

function add_it( $insertion, $paragraph_id, $content ) {
    $close_p = '</p>';
    $paragraphs = explode( $close_p, $content );
    foreach ($paragraphs as $index => $paragraph) {

        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $close_p;
        }

        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
    
    return implode( '', $paragraphs );
}

add_filter( 'the_content', 'place_adv' );

Simply insert this code to your theme's functions.php file. It will display the ads after 2nd para of each post.

You can also use Insert Post Ads plugin to insert ads inside your WordPress post. Just install and activate the plugin. You will see a Post Adverts on the left.

Go to Post Adverts » Add New. On this page enter title, ads code, select the position and then click on publish.




Next go to the Post Adverts » Settings and select post type in which you want to insert ads.


That's it. Hopefully, it will help you to achieve your goal.
Previous
Next Post »
0 Comment