Insert ads into Post Content without plugin

How to Insert ads into Post Content?

Insert ads into post content | In this article we are going to learn how can we display google or any others ads into your article without using any plugin. Let’s do it by following simple two step :

Step 01 : Go to your current/activated theme folder and the the following code to your functions.php file:

// Insert ads code into post content
// $adsCode: Specify code that wants to add
// $insertAfter: Specify paragraph number

add_filter('the_content', 'fwp_insert_post_ads');
function fwp_insert_post_ads($content){
        //ads code
        $adsCode = '<div>Insert your ads code or html element between this div tag</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;    

Step 02 : In this step here you only need to do two thing which are changing the value of the following variables :

  1. $adsCode – Insert the ads code from Google or any other ads networks. Here you can also add any kind of Banner or HTML elements which you want to insert into the post content.
  2. $insertAfter – In this step you will need to specify where you want to display your ads in your post content. Your ads will be placed after the specified paragraph. If you specify 2 as the value of the variable in this section that means your ads will be appear after 2 paragraphs of your post/content. Thanks for reading the full post.

N.B. This is a developer-level tutorial. If you feel, it’s difficult to do for you. You may consider hiring me through Fiverr. I’ll do it for you.

Table of Contents :

Leave a Comment

Your email address will not be published.