Place AdSense Code after Specific Paragraph [Not Working]

Community Forums Forums Design Tips and Tricks Place AdSense Code after Specific Paragraph [Not Working]

This topic is: resolved

This topic contains 4 replies, has 3 voices, and was last updated by  Borange 1 year, 11 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #62014


    There’s a great post here by Brad Dalton about how to place some code in your functions.php file to get Adsense code to appear after a specific paragraph. I’ve managed to get the Adsense area to display correctly, but the ad itself isn’t displaying; it just shows blank. In the area for

    $new_content .= ‘Paste Adsense Code Here';

    was I not supposed to include the <script type=”text/javascript”> info? Here’s what I actually have in my functions.php file:

    add_filter( 'the_content', 'wpsites_adsense_middle_content' );
    function wpsites_adsense_middle_content( $content ) {
            if( !is_single() )
                return $content;
                $paragraphAfter = 1; //Enter paragraph number
                $content = explode ( "</p>", $content );
                $new_content = '';
                    for ( $i = 0; $i < count ( $content ); $i ++ ) {
                        if ( $i == $paragraphAfter ) {
                        $new_content .= '<div style="width: 310px; height: 260px; padding: 5px 5px 5px 0; margin-right: 0; float:right">';
                        $new_content .= '<script type="text/javascript"><!--
    google_ad_client = "ca-pub-XXXXXXXXXXXXX";
    /* AD-NAME */
    google_ad_slot = "XXXXXXXXXXX";
    google_ad_width = 300;
    google_ad_height = 250;
    <script type="text/javascript"
                        $new_content .= '</div>';
                $new_content .= $content[$i] . "</p>";
                return $new_content;

    I adjusted the width of the div to accommodate a 300×250 ad plus padding. I can adjust that setting accordingly and see the results on the spacing of the text, but again, the ad itself is just showing blank.



    Sometimes Google takes time to approve add placement but that may not be the reason if you’ve already waited.

    Or you could try to register the script and put it in a js folder then use the handle in the code above using wp_enqueue_script.

    Another option is to use the code to create a new widget area and use an Adsense plugin to display the code.



    Just closing the loop on this: it turned out that the code worked just fine as I pasted it above. Make sure the entire AdSense javascript bit is contained inside the single quotation mark. I tested it with several different AdSense codes, and it worked just fine with every one that was Active. If AdSense lists the ad slot as “New” it won’t work — it has to be shown as Active. Thanks for this great code, Brad!



    Will this ad placement penalize you having an ad in the content?

    I was wondering if you can do a wrap around ad in content sot he text wraps around the ad?



    I added this to my site but it looks odd, is there anyway to make the TEXT in the article wrap around the adsense block?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.