How to Insert Ads in your WordPress Post without a Plugin

In content advertisements are becoming more and more popular, and the most obvious reason for that is the fact that they adapt very well and get a high click-through-rate (CTR) which is very important for any advertiser. Because of that, you probably want to place your ads in the post content of your WordPress blog.

There are several ways to achieve that. The worst option of course is to manually insert them in every post. This is time consuming at first, and secondly once you need to change the code, it will cost you even more time.

The second way is by searching for a WordPress plugin that can do that for you. Since there are WordPress plugins for almost anything nowadays, you probably will find one. However, if you are type of person who does not want to fill up their WordPress website with too many different plugins, you probably want to do it without a plugin.

In this post we are going to teach you how to easily insert ads in your WordPress without installing a plugin. That can easily be done with just a few lines of code in your functions.php file. The main assumption is that you have use paragraphs in your posts. Paragraphs split the content and make it easier to read. Because of that, in our code we are going to place the ad block after a specific number of paragraphs.

How to do insert ads in your WordPress post

So, let’s do it. Go to your admin dashboard and click on Appearance > Theme Editor.

Wordpress Appearance and Theme Editor

Once you are in your theme editor, click on the functions.php file on the right. Then, add the following code right at the end of the file:

// Insert ads after first paragraph of single post content.
 
add_filter( 'the_content', 'prefix_insert_post_ads' );
 
function prefix_insert_post_ads( $content ) {
     
    $ad_code = '<div>Ad code goes here</div>';
 
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 1, $content );
    }
     
    return $content;
}
  
// Function that injects the ads
  
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 );
}

Before saving your new functions.php file, you have to do two edits. Firstly, on the line 7 you need to edit the $ad_code and add your add code. Secondly, on the line 10, you can edit the number “1” and change it with the number after which paragraph you want your add to be shown.

And that’s it! Click on “Update” and your ads will be shown after the paragraph you selected.

We hope you liked our article, and if you have any questions or comments, please leave it in the comment section under the article. Feel free to explore our other WordPress tutorials for your blog. For more news follow us on our Facebook and Twitter profiles.

Leave a Reply

Your email address will not be published.