Functions – Individual ads on each website on a multi-site environment

I’m using the following code to insert an ad in the text via the functions.php file, after the 8th paragraph:

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, 8, $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 );
}

Each website on the network has a different ad code, but at the same time I am using the same theme on all sites, thus – only one function.php file. I know I can create multiple child-themes for every installation, but that’s not an option here.

Is there a way to use the above code for a multisite environment, but only to add the code to a single, common functions.php file?

Thank You…

Leave a Comment