Easy Way to Insert Ads Within Post Content in WordPress

insert-ads-within-posts-featured

From time and time we see a lot of users who wish to insert ads within post content in their WordPress site as to increase their earnings. But for a beginner, inserting ads within post content is quite a task if they don’t know how to do it properly.

Actually, most beginners add their ad code directly into the post to achieve this functionality.

But this approach is really inefficient in the long run as you have to manually edit a lot of posts. So, let us see an easy way to insert ads within post content in WordPress using a simple code snippet and a plugin.

So, let us see an easy way to insert ads within post content in WordPress using a simple code snippet and a plugin.

Insert Ads Using a Simple Code Snippet

The easiest way to insert ads within post content is by using the below code snippet. Just copy and paste the below code snippet into your theme’s functions.php file or site specific plugin.

// Insert Ads or other content within post content
add_filter( 'the_content', 'bs_inpost_ads_1' );
function bs_inpost_ads_1( $content ) {
        if( !is_single() )
            return $content;
            $paragraphAfter = 0; //Enter paragraph number
            $content = explode ( "</p>", $content );
            $new_content = '';
                for ( $i = 0; $i < count ( $content ); $i ++ ) {
                    if ( $i == $paragraphAfter ) {
                    $new_content .= '<div style="width: auto; min-height: 70px; padding: 5px 10px 0 0; float:left;">';
                    $new_content .= 'YOUR AD CODE';
                    $new_content .= '</div>';
                    }
            $new_content .= $content[$i] . "</p>";
            }
            return $new_content;
    }

Once you have done that, replace “YOUR AD CODE” in line 14 with your actual ad code.

That’s all there is do. From this point forward, your ads will appear within the post content. For further customization, please continue reading.

insert-ads-within-posts-ad-disply

Customizations

By default, this code snippet adds your ad code at the top of the post. If you want to insert the ad after a certain number of paragraphs, then change the number in line 8 accordingly.

Also, by default the ad will float to the left side of the content. If you can want, you can declare your own CSS in line 13.

To center the ad within the post, remove the CSS declaration float: left; and add text-align: center; declaration.

That being said, if you are really uncomfortable using code snippets or if you want more control over the overall ads displayed in your site, just use a free plugin called Insert post ads.

Hopefully that helps and do comment below if you face any problems while adding the above code snippet.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.