WordPress How to Insert Ads On Middle Article Post

WordPress How to Insert Ads On Middle Article Post

This Script make Ads, ADSENSE or another in middle position post by paragraph

ok just do it first edit /wp-content/themes/yourtheme/functions.php


//Insert ads after second paragraph of single post content. function prefix_insert_after_paragraph2( $ads, $content ) {
    if ( ! is_array( $ads ) ) {
        return $content;
    }

    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );

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

        $n = $index + 1;
        if ( isset( $ads[ $n ] ) ) {
            $paragraphs[$index] .= $ads[ $n ];
        }
    }

    return implode( '', $paragraphs );
}

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
    if ( is_single() && ! is_admin() ) {
        $content = prefix_insert_after_paragraph2( array(
            // The format is: '{PARAGRAPH_NUMBER}' => 'AD_CODE',
            '1' => 'YOUR ADS FIRST PARAGRAPH HERE',
            '2' => 'YOUR ADS SECOND  PARAGRAPH HERE',
        ), $content );
    }

    return $content;
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *