Don't count <p> inside a blockqoute

Hello guys! I use a function that inserts an ad after X number of paragraphs. I want to make the function to ignore blockquotes and don’t count them as paragraphs.

add_filter( 'the_content', 'add_ads_to_content' );

function add_ads_to_content( $content ) {

    $ads = array(
        2 =>  'ad code 1', // paragraph_id => ad_code
        4 => 'ad code 2', // paragraph_id => ad_code
        6 => 'ad code 3' // paragraph_id => ad_code
    );

    if ( is_single() && ! is_admin() ) {
        foreach ($ads as $paragraph_id => $ad_code) {
            $content = prefix_insert_after_paragraph( $ad_code, $paragraph_id, $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 );
}
<p>
    Paragraph
    <p>Sub paragraph</p>
</p>
<p>Another paragraph</p>

In this example, would the sub paragraph count as a paragraph?

yes, it would count as a paragraph

Sponsor our Newsletter | Privacy Policy | Terms of Service