How to Insert Content/Ads After nth Paragraph in WordPress

It is common to show a piece of content, or specific page element in the middle of each blog post on your WordPress website. The piece of content can be table of content, ads, or related articles.

Add these code in your plugin:

add_filter( 'the_content', 'myplugin_inject_content' );
function myplugin_inject_content( $content ) {

    $additional_content = '<div>My ads or content</div>';

    if ( is_single() ) {
        $paragraph_position = 0; //position start from 0
    	$content = myplugin_prefix_insert_after_paragraph( $additional_content, $paragraph_position, $content );      

    return $content;
function myplugin_prefix_insert_after_paragraph( $additional_content, $paragraph_position, $content ) {
    $paragraphs = explode( '</p>', $content );
    foreach ($paragraphs as $key => $paragraph) {
        //add closing p back
        if ( trim( $paragraph ) != '' ) {
            $paragraphs[$key] .= '</p>';
        //add additional content at the wanted position 
        if ( $paragraph_position == $key ) {
            $paragraphs[$key] .= $additional_content;

    return implode( '', $paragraphs );

If you don’t want to code, you can install any Ads manager plugins. Most of them allow where the content can be showed on a blog post.

Leave a Comment

Your email address will not be published.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.