Adding addcode at the center based on number of paragraphs

I have my website setup to display an add on the 7th paragraph. Because not every post is as long as the other this can cause some weird ad placements.

Instead I would like to use the following logic:

Read More

If number_of_paragraphs => 14 then paragraph_insert_id= number_of_paragraphs / 2. Else nothing.

//adding mid post add based on paragraph
add_filter( 'the_content', 'prefix_insert_post_ads_mid' );

function prefix_insert_post_ads_mid( $content ) {

    $ad_code = '<div id="adsensemid">code goes here</div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, $content );
    }

    return $content;
}


function prefix_insert_after_paragraph( $insertion, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    // floor or ceil; to make it round
    $mid = ceil(length($paragraphs) / 2);
    foreach ($paragraphs as $index => $paragraph) {

        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        if ( $mid == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }

    return implode( '', $paragraphs );
}

Unfortunately my first modifications already broke the script. When applied to the website all content is blank. Is there someone that can find the flaw? Any help would be hugely appreciated!

ps current working script:

//adding mid post add based on paragraph
add_filter( 'the_content', 'prefix_insert_post_ads_mid' );

function prefix_insert_post_ads_mid( $content ) {

    $ad_code = '<div id="adsensemid></div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 7, $content );
    }

    return $content;
}


function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</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 );
}

Related posts

1 comment

  1. I have identified the issue. The code is now fully functional.

    $mid = ceil(count($paragraphs) / 2);
    

    Full code example:

    //adding mid post add based on paragraph
    add_filter( 'the_content', 'prefix_insert_post_ads_mid' );
    
    function prefix_insert_post_ads_mid( $content ) {
    
        $ad_code = '<div id="adsensemid">code goes here</div>';
    
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $ad_code, $content );
        }
    
        return $content;
    }
    
    
    function prefix_insert_after_paragraph( $insertion, $content ) {
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        // floor or ceil; to make it round
        $mid = ceil(count($paragraphs) / 2);
        foreach ($paragraphs as $index => $paragraph) {
    
            if ( trim( $paragraph ) ) {
                $paragraphs[$index] .= $closing_p;
            }
    
            if ( $mid == $index + 1 ) {
                $paragraphs[$index] .= $insertion;
            }
        }
    
        return implode( '', $paragraphs );
    }
    

    I did encounter a flaw in it’s design. If you use pagebreaks (nextpage tag) then this script will still count the entire content. So let’s say you have a post that has two pages, each with 4 paragraphs. The script will count 8 and throw itself after the 4th paragraph. Because the article is split it won’t be the center, instead it will be the last paragraph of each of the two pages.

    So for now I have modified the original code to the following conditions
    paragraphs <= 8 display nothing
    paragraphs <14 find true middle (like the code above)
    else use the 7th paragraph

    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        $number_of_paragraphs = ceil(count($paragraphs));
        if ($number_of_paragraphs <= 8 ) {
            //too small to display middle add
            $insertion = '';
        } elseif ( $number_of_paragraphs <= 14 ){ 
         //too short for 7th paragraph, find true center
        $mid = ceil(count($paragraphs) / 2);
        $paragraph_id = $mid ;
        }
    
        foreach ($paragraphs as $index => $paragraph) {
    
            if ( trim( $paragraph ) ) {
                $paragraphs[$index] .= $closing_p;
            }
    
            if ( $paragraph_id == $index + 1 ) {
                $paragraphs[$index] .= $insertion;
            }
        }
    
        return implode( '', $paragraphs );
    }
    

    If anyone has a solution to find the true center of posts that are split between multiple pages (I have up to 8 pagebreaks!) then please let me know.

Comments are closed.