How To Wrap Google Adsense In Wordpress Posts Correctly
Spread it!
It’s easy to add Google Adsense code in the top, the bottom of WordPress posts by edit your WordPress theme posts file. However, sometime you need to wrap Google Adsense code in WordPress posts, and you can not do as the same way as add it on top, bottom. This article will explain you how to do that.
For coupe of months ago, I published a post Wordpress Hack: Anything Can Be Added Anywhere In The Post Content. With that tutorial, you can add anything in WordPress posts on top, bottom before other plugins do, even right below the post excerpt.
Updated: Thanks to Jonathan Foucher and Nathan J. Brauer for notify me the replacement function. I need to visit more often on php manual site.
1. Replacement using str_replace() function
The only one way for you to insert Google Adsense or anything in WordPress Posts is using str_replace() function to scan the whole content for the string like <!-googlead-> then replace it by the Google Adsense code.
First, create a function like this one below in functions.php file:
function googleadsense($content){
$adsensecode = 'GOOGLE ADSENSE GOES HERE';
$pattern = '<!-googlead->';
$content = str_replace($pattern, $adsensecode, $content);
return $content;
}
add_filter('the_content', 'googleadsense');
That’s all you need. Now, whenever you publish a post, insert <!-googlead-> in post content where you want Google Adsense to be displayed.
2. Using Shortcode API
From version 2.5.1, WordPress has supported Shortcode API, a simple set of functions for creating macro codes for use in post content. We need to write a function which is a shortcode handler function in funtions.php file. The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro.
In this case, we will use the shortcode [googlead] to insert Google Adsense code in WordPress post. All the code we need is:
function googlead_shortcode() {
$adsensecode = 'GOOGLE ADSENSE SHORCODE GOES HERE';
return $adsensecode;
}
add_shortcode('googlead', 'googlead_shortcode');
Refer to the code above, when the_content is displayed, the shortcode API will parse any registered shortcodes such as [googlead]. The return (Google Adsense code) by the shortcode handler will be inserted into the post body in place of the shortcode [googlead] itself.
In my conclusion, these two code snippets above is the simplest and easiest to insert wrap Google Adsense code in WordPress posts. However, you sometime need to change the Google Adsense code to display, you should use the WordPress Option value for Google Adsense code instead of adding the ad code in functions.php file. Have you tried to add options for your WordPress theme before? If not, please take a look at this post: Wordpress Theme Design with Options Adminstration. Hope this post is useful for you!
33 User Comments
Trackbacks/Pingbacks
-
-
21. Jan, 2010
[...] This post was mentioned on Twitter by Sergio Arantes, Web Design News. Web Design News said: How-to Wrap Google Adsense In Wordpress Posts Correctly http://bit.ly/6fEwXM [...]
-
-
21. Jan, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]
-
-
22. Jan, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]
-
-
23. Jan, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]
-
-
23. Jan, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]
-
-
25. Jan, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly | AEXT.NET (tags: wordpress adsense google hacks articles tips) [...]
-
-
26. Jan, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]
-
-
07. Feb, 2010
[...] Source : aext.net [...]
-
-
18. Feb, 2010
[...] this article: How To Wrap Google Adsense In Wordpress Posts Correctly | AEXT.NET Tags: [...]
-
-
08. Mar, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]
-
-
12. Mar, 2010
[...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]











i hope it will help me
hi, thanks for the tutorial. right now i am using some plugins to insert Adsense on my post but i think this is the most easiest one and no need to add additional codes.
Wow…just a simple function and voila! …thanks man!
ereg is THE slowest replacement function in PHP. You should never use it, even if you need regex capabilities. In your case its just plain stupid, because you don’t need a regex at all!!
a simple
str_replace('','ADSENSE CODE', $content);would be faster and do the exact same thing. Please PLEASE, read at least this post : http://www.hm2k.com/posts/50-php-optimisation-tips-revisited to avoid doing similar mistakes in the future.grr, ate my code… it was meant to say :
str_replace('<!-googlead->','ADSENSE CODE', $content);I appreciate your comment Jonathan. My big mistake is visit the update from php manual site seldom. No more mistake in the future. Thanks!
Hey nice tutorial, but ereg_replace() was depreciated as of PHP 5.3.0 and removed as of PHP 6.0.0. You should revise the tutorial with an alternate method
This should work just fine in this case:
$content = str_replace($pattern, $adsensecode, $content);Very clear and concise piece Lam. This post has certainly urged me to make the switch to WP from EE.
Come one, stay with your EE, Joh! You are doing great!
I still haven’t got my Adsense codes. But when I do, I shall definitely come back to this post.
Bookmarked!! Thanks a lot, Lam
I really really hate using google ads on websites there is so many places to get better looking ads online. buy sell ads, Cj.com and alot more.
julio´s last blog ..16 Websites to display your Creative Work
thank u very much nguyen, i will to try
Lynn´s last blog ..Complete Customization and SEO Guide for Blog / Website
thank you for this tips.. very useful
Let’s me try on my Blog.. thanks for share
IndoDX´s last blog ..TotalFinder: Your Apple Finder Solutions
thanks for the tip buddy

sriganesh´s last blog ..Win a TutsPlus Membership
How do you put that box that say “ENJOY THE ARTICLE,? BLENDIT ” to the left or related post ?
I like the way it looks!
arquigrafico´s last blog ..Descarga gratis bloques Bibliocad coloreados de Autocad
Edit the related posts template file.
bad code

seo´s last blog ..Playstation 3 Kırıldı
Why don’t you provide another better way? The comment form here is not restricted for a longer sentence like “bad code”
Nice tutorial. I know there are many plug-ins in Wordpress to do just this. But having in control of your own code is a far better thing. Thanks

TechChunks´s last blog ..Got 1500 Facebook/Twitter Friends? Your Brain Can’t Even Handle 150
Thnks for your tip really usefull mate

Sid´s last blog ..5 Most Effective Tricks to become a Successful Blogger
Thanks for this! Was nicely explained, alot of tutorials on the internet now seem to be hard to follow, so major well done!