Function – Advertisement on only one website inside multisite network – Code error

I made this code snippet for functions.php to post ads only on blogs with id 2 inside WP Multisite, but it doesn’t work. I tried couple edits and fractions – always same error. Can anyone shed light on this? Thank you

Error Explanation:

When the code is activated, it works, but other sites on the multisite don’t show/render the content properly – eg, white screen of death. On this site, everything works fine, but the rest of the network is “down”

Code:

add_filter( 'the_content', 'prefix_insert_post_ads' );
  
function prefix_insert_post_ads( $content ) {
    if ( 2 !== get_current_blog_id() )
        return;
      
    $ad_code="<div>Ads code goes here</div>";
  
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 8, $content );
    }
      
    return $content;
}
   
   
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    if ( 2 !== get_current_blog_id() )
        return;
    $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 );
}

Leave a Comment