• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Place AdSense Code after Specific Paragraph [Not Working]

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

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

This topic is: resolved
  • This topic has 4 replies, 3 voices, and was last updated 11 years, 7 months ago by Borange.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • September 11, 2013 at 3:43 pm #62014
    jmrallen
    Member

    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>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>';
                        $new_content .= '</div>';
                        }
                $new_content .= $content[$i] . "</p>";
                }
                return $new_content;
        }

    I adjusted the width of the div to accommodate a 300x250 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.

    September 12, 2013 at 12:10 am #62060
    Brad Dalton
    Participant

    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.


    Tutorials for StudioPress Themes.

    September 13, 2013 at 12:54 pm #62323
    jmrallen
    Member

    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!

    September 13, 2013 at 4:42 pm #62392
    Borange
    Member

    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?

    December 5, 2013 at 4:40 pm #77296
    Borange
    Member

    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?

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble