Community Forums › Forums › Archived Forums › Design Tips and Tricks › Place AdSense Code after Specific Paragraph [Not Working]
- This topic has 4 replies, 3 voices, and was last updated 11 years, 3 months ago by
Borange.
-
AuthorPosts
-
September 11, 2013 at 3:43 pm #62014
jmrallen
MemberThere'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 #62060Brad Dalton
ParticipantSometimes 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.
September 13, 2013 at 12:54 pm #62323jmrallen
MemberJust 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 #62392Borange
MemberWill 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 #77296Borange
MemberI added this to my site but it looks odd, is there anyway to make the TEXT in the article wrap around the adsense block?
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.