Place ads before last paragraph in wordpress

Here is the code for place ads after 1st, 2nd, 3d paragraph. but i want to place ad just before last paragraph of my wordpress post. is there any way to do this ?

<?php

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

    return $content;
}

// Parent Function that makes the magic happen

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

Related posts

Leave a Reply

3 comments

  1. add_filter('the_content', 'ad_signal');
    function ad_signal($content)
    {
        if (!is_single()) return $content;
        $tent = get_the_content();
        $content = explode("</p>", $content);
        $ss = count($content);
        $ns = $ss-1;   //**before last paragraph in wordpress  **
        $new_content = '';
        for ($i = 0; $i < count($content); $i++) {
             if ($i == $ns ) {
    
                $new_content.= ' <div style="text-align:center;">';
                $new_content.= '<h1> Your ads Here ! </h1>';
                $new_content.= '</div>';
            }
    
    
            $new_content.= $content[$i] . "</p>";
        }
    
        return $new_content;
    }
    
  2. I have set Adsense code by this Code in my sites named All BD Results and etunescafe. You just add this code in your sites functions.php N.B. Before adding the code please replace your or add your adsense code. It will be good enough to go through this post. How to Set Adsense Ads Right Before The Last Paragraph

    function ads_added_above_last_p($text) {
        if( is_single() ) :
            $ads_text = '<div class="a" style="text-align: center;">Your Adsense Code</div>';
            if($pos1 = strrpos($text, '<p>')){
                $text1 = substr($text, 0, $pos1);
                $text2 = substr($text, $pos1);
                $text = $text1 . $ads_text . $text2;
            }
        endif;
        return $text;
        }
    add_filter('the_content', 'ads_added_above_last_p');
    
  3. Hey guys so I ran into the same problem and managed to solve it incorporating your answers into one.

    The function that does all the processing is changed so that is counts all the paragraphs created by the explode method. Then another if statement is added to evaluate when you are at the desired location.

    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    
        $closing_p = '</p>';
    
        $paragraphs = explode( $closing_p, $content );
    
        $last_paragraph_index = count($paragraphs);
    
        foreach ($paragraphs as $index => $paragraph) {
    
            if ( trim( $paragraph ) ) {
                $paragraphs[$index] .= $closing_p;
            }
    
            if ( $paragraph_id == $index + 1 ) {
                $paragraphs[$index] .= $insertion;
            }
    
            else if( $last_paragraph_index + $paragraph_id == $index + 1){
                 $paragraphs[$index] .= $insertion;
            }
    
        }
    
    
        return implode( '', $paragraphs );
    }
    

    in order to use the new functionality you must call it with a negative number.

    Think of the function this way, if you want to go from the top of the content you use a positive number, if you want to go from the bottom of the content you use a negative number.

    Remember that even though the evaluation is for your decided spot such as before the last paragraph the condition that adds to the array uses the index which we all know is zero based.

    This just means that to get after paragraph 1 you must use the number 2 when calling the function and to get to before the last paragraph you have to use -2 and so on.

    Here is my example to add the a read the next post if it exists to the article

    add_filter( 'the_content', 'prefix_insert_next_article_banner' );
    
    function prefix_insert_next_article_banner( $content ) {
    
        if ( is_single() && ! is_admin() && get_next_post_link() ) {
    
               $next_banner  = '<div class="next-banner"> <span> Read Next </span>';
               $next_banner .= '<a href="' . $prev = get_permalink(get_adjacent_post(false,'',false)) .'"> ' . get_the_title(get_adjacent_post(false,'',false)) .' </a>';
               $next_banner .= '</div>';
    
               return prefix_insert_after_paragraph( $next_banner, -2, $content );
    
        }
    
        return $content;
    }