Search for a plugin or theme

How to Insert Ads and Content in Any Part Within Your WordPress Post

How to Insert Ads and Content in Any Part Within Your WordPress Post

In this tutorial, I’m going to show you how to insert ads, images, text, or any other content to any part of your WordPress blog post without using a plugin.

We will focus on three breakpoints, the beginning, middle, and the end of the post.

I will provide you with three separate codes, one code for each of the post parts, and also one big code at the end to use it all together at the same time.

Also, at the end of this article, I’ll provide you with the template and common code snippets to various locations to insert your content.

How to Insert Ads or Content to WordPress Posts

The codes below need to be pasted into the functions.php file of your child theme or a site-specific plugin.

Let’s start with the first location and insert the ads or other content at the beginning of the post.

Beginning of the Post

To insert ads or any other content to the beginning of the post (before the first paragraph) use the code below:

function pfwp_insert_content_into_post($content){
	if (is_singular('post')) {  
		$beforePostContent = '<p>Content at the beginning of the post</p>';
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        
		foreach ($paragraphs as $index => $paragraph) {
 
			if ( trim( $paragraph ) ) {
				$paragraphs[$index] .= $closing_p;
			}

			if ( $index == 0 ) {
                $paragraphs[$index] = $beforePostContent . $paragraph;
            }
		}
        $content = implode( '', $paragraphs );
	}
    return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );

End of the Post

To insert ads or any other content to the end of the post (after the last paragraph) use the code below:

function pfwp_insert_content_into_post($content){
    if (is_singular('post')) {
        $afterPostContent = '<p>Content at the end of the post</p>';
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
		
		foreach ($paragraphs as $index => $paragraph) {
 
			if ( trim( $paragraph ) ) {
				$paragraphs[$index] .= $closing_p;
			}

			if ( count($paragraphs) == $index + 1 ) {
                $paragraphs[$index] .= $afterPostContent;
            }
		}
        $content = implode( '', $paragraphs );
	}
    return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );

Middle of the Post

To insert ads or any other content exactly in the middle of the post use the code below:

function pfwp_insert_content_into_post($content){
	if (is_singular('post')) {  
        
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );

        // contet to add number 1
        $insertContent_1 = '<p>Content in the middle of the post</p>';
        $position_1 = floor(count($paragraphs) / 2);
        
		foreach ($paragraphs as $index => $paragraph) {
 
			if ( trim( $paragraph ) ) {
				$paragraphs[$index] .= $closing_p;
			}
            if ( $position_1 == $index + 1 ) {
                $paragraphs[$index] .= $insertContent_1;
            }
		}
        $content = implode( '', $paragraphs );
	}
    return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );

More Common Positions

To insert ads or any other content to the below positions, use the mid-post template (the template above) and just replace line 7 ($position_1 = floor(count($paragraphs) / 2)) with the relevant position from the list below.

After the First Third of the Post

$position_1 = floor(count($paragraphs) / 3);

After the Second Third of the Post

$position_1 = floor(count($paragraphs) / 1.5);

After the First Paragraph of the Post

$position_1 = 1;

After the Second Paragraph of the Post

$position_1 = 2;

One Paragraph Before the End of the Post

$position_1 = floor(count($paragraphs) - 2);

Two Paragraphs Before the End of the Post

$position_1 = floor(count($paragraphs) - 3);

Insert into Multiple Areas of the Post

Many of you would like to combine some of the positions above and insert ads or other content in multiple positions of the posts.

For example, ads at the beginning of the post and images in the middle of the post.

For that, I’ve created the template below and tried to keep it as simple and easy as possible for non-developers.

The template below will insert content to the beginning, middle, and end of the post. You can leave $beforePostContent or $afterPostContent empty (like so: $beforePostContent = '';) if you don’t want any content to be added there.

Feel free to play with the mid-post position based on your needs. You can use the code snippets I mentioned above. Also, you can add more positions to the code if you desire. For example, content to the first third and the second third of the post (you can also watch the video for clarification).

function pfwp_insert_content_into_post($content){
	if (is_singular('post')) {  
		$beforePostContent = '<p>Content at the beginning of the post</p>';
        $afterPostContent = '<p>Content at the end of the post</p>';
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        
        // Contet number 1
        $insertContent_1 = '<p>Mid post content goes here</p>';
        $position_1 = floor(count($paragraphs) / 2);
		
		foreach ($paragraphs as $index => $paragraph) {
 
			if ( trim( $paragraph ) ) {
				$paragraphs[$index] .= $closing_p;
			}

			if ( $index == 0 ) {
                $paragraphs[$index] = $beforePostContent . $paragraph;
            }

            // Add content number 1 to anywhere in the post
            if ( $position_1 == $index + 1 ) {
                $paragraphs[$index] .= $insertContent_1;
            }

			if ( count($paragraphs) == $index + 1 ) {
                $paragraphs[$index] .= $afterPostContent;
            }
		}
        $content = implode( '', $paragraphs );
	}
    return $content;
}
add_filter( "the_content", "pfwp_insert_content_into_post" );

Feel free to watch the video to better understand how it’s working. If you need another position that I did not mention in this post please leave a comment down below and I’ll add it.

Conclusion

In this article, you learned how to insert content in various places in your WordPress blog posts. Now, it’s a good time for you to copy some of the codes above and make good use of them.

Leave a comment below and let us know which code you’re using.

Also, make sure to subscribe to our YouTube channel and like our page on Facebook.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email

PluginsForWP

PluginsForWP is a website specializing in redistributing WordPress plugins and themes with a variety of knowledge about WordPress, internet marketing, and blogging.

Leave a Reply