How to put Ads in a Post without plugin

How to put Ads in a Post without plugin

Now if you don’t like using the plugin but you still want to place ads in between your post. The good news is that you can still have it with series of codes just follow the directions below.

NOTE:- Remember we’ve written on how to put ads on in between your blog post using plugins

Open your theme’s functions.php or a site-specific plugin file and paste the following code:

//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
	$ad_code = '
<div>Ads code goes here</div>


	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $content );
	return $content;
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
	$closing_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 );

To add your ads code, just go ahead and edit $ad_code value where it says “Ad code goes here” on line 9. After doing that, you are done. To change the number of paragraphs, simply change the number 2 to another paragraph number on line 12.

Related Post

Also Read  How to Move WordPress to a New Host or Server With No Downtime

Leave a Reply

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

Close Menu