How to Insert Google AdSense Ads within or inside your Post Content in WordPress without plugin?
It’s a very common thinking to increasing google adsense revenue, for this you should place you google ads where it get more impression and more clicks. It’s my personal experience putting adsense in between post is very effective for getting more impression and click, because this is the area of your website where user stay most of the time.
When i start blogging i was wondered like other beginners, whether these sites insert google ads manually when they write the content, or if there is a special code or plugin for this?
To achieve this i have used many plugin, but believe me, finally at the last i do this by a small code snippets,
and recommended to you don’t need to install any heavy plugin for a small task, you can put your google adsense inside your post content with this magic code.
Here i have inserted google adsene after 4th paragraph(after closing of </p> tag). Normally a standard post can have 7-10 paragraph at least. You can change this number according to your blog or post content length.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//Insert ads after 4th paragraph of single post content. add_filter( 'the_content', 'w3reign_post_adsense' ); function w3reign_post_adsense( $adsContent ) { $adsCode = '<div>Paste your AdSense code here</div>'; if ( is_single() && ! is_admin() ) { return ads_insert_after_paragraph( $adsCode, 4, $adsContent ); } return $adsContent; } // Parent Function that makes the magic happen function ads_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 ); } |
[paypal-donation]
Thank you, that is exactly what I needed.
Thanks for the code!
How can I exclude certain posts?
Hi Martijn,
The above code snippets is standard and common for all post pages.
WOW…!, Perfect solution, exactly what i looking for.