0

I am using following code to insert ad in article, after 8th paragraph, through functions.php file:

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 );
}

Every webisite on network has different ad code, but in the same time I am using same theme on all sites, thus - only one functions.php file. I know I can create multiple child-themes for every installation, but that is not an option here.

Is there any way to use above code for multisite environment, but to add code to only one, general functions.php file?

Thank you...

7
  • I'm not adding an answer as this is just a plugin suggestion. Have you heard of this plugin? wpclips.net/adding-custom-code-in-wp-multisite
    – Lisa
    Commented Oct 31, 2022 at 0:23
  • 1
    @Lisa thank you - I already used this plugin on other projects, and Code Snippets plugin as well. In this case I only can arrange this through functions.php file, without plugin usage.
    – ZeusDev
    Commented Oct 31, 2022 at 1:38
  • Sorry! Wish I could help more.
    – Lisa
    Commented Oct 31, 2022 at 3:19
  • 1
    @Lisa thanx :) I appreciate
    – ZeusDev
    Commented Oct 31, 2022 at 16:39
  • Why don't you add an option for the theme to provide the AD account ID. Then, you retrieve the ID for each site and run the AD code for each site, but now you're loading the identifier that makes them different via a variable that you've pulled from each site's settings. This way you're still just running one function for the theme and can re-use that function site-to-site. Commented Nov 4, 2022 at 21:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.