How To Wrap Google Adsense In Wordpress Posts Correctly

How To Wrap Google Adsense In Wordpress Posts Correctly


Spread it!

  • Share

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!

  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • Share on your Facebook
  • Submit this post to Dzone
  • Submit this post to Designbump
  • Submit this post to TheWebBlend

Author: Lam Nguyen

I'm Lam Nguyen, a 21 year old web developer writing about everything related to web design. I am owner of AEXT.NET and WhoFreelance.com Web Community News. You can catch me on twitter.


33 User Comments

  1. lawrence77 21. Jan, 2010 at 1:15 am #

    i hope it will help me ;)

  2. annanta 21. Jan, 2010 at 1:23 am #

    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.

  3. Madalin 21. Jan, 2010 at 1:49 am #

    Wow…just a simple function and voila! …thanks man!

  4. Jonathan Foucher 21. Jan, 2010 at 2:21 am #

    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.

    • Jonathan Foucher 21. Jan, 2010 at 2:22 am #

      grr, ate my code… it was meant to say :
      str_replace('<!-googlead->','ADSENSE CODE', $content);

    • Lam Nguyen 21. Jan, 2010 at 3:54 am #

      I appreciate your comment Jonathan. My big mistake is visit the update from php manual site seldom. No more mistake in the future. Thanks!

  5. Nathan J. Brauer 21. Jan, 2010 at 2:24 am #

    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);

  6. Josh 21. Jan, 2010 at 3:01 am #

    Very clear and concise piece Lam. This post has certainly urged me to make the switch to WP from EE. :)

    • Lam Nguyen 21. Jan, 2010 at 3:35 am #

      Come one, stay with your EE, Joh! You are doing great!

  7. Richie 21. Jan, 2010 at 4:02 am #

    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

  8. julio 21. Jan, 2010 at 8:34 am #

    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 My ComLuv Profile

  9. Lynn 21. Jan, 2010 at 7:33 pm #

    thank u very much nguyen, i will to try
    Lynn´s last blog ..Complete Customization and SEO Guide for Blog / Website My ComLuv Profile

  10. denbagus 21. Jan, 2010 at 10:19 pm #

    thank you for this tips.. very useful

  11. IndoDX 24. Jan, 2010 at 6:06 am #

    Let’s me try on my Blog.. thanks for share
    IndoDX´s last blog ..TotalFinder: Your Apple Finder Solutions My ComLuv Profile

  12. sriganesh 24. Jan, 2010 at 9:25 am #

    thanks for the tip buddy :)
    sriganesh´s last blog ..Win a TutsPlus Membership My ComLuv Profile

  13. arquigrafico 24. Jan, 2010 at 10:34 pm #

    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 My ComLuv Profile

    • Lam Nguyen 24. Jan, 2010 at 10:35 pm #

      Edit the related posts template file. :D

  14. seo 27. Jan, 2010 at 1:43 am #

    bad code :)
    seo´s last blog ..Playstation 3 Kırıldı My ComLuv Profile

    • Lam Nguyen 27. Jan, 2010 at 3:27 am #

      Why don’t you provide another better way? The comment form here is not restricted for a longer sentence like “bad code” :)

  15. TechChunks 28. Jan, 2010 at 10:13 am #

    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 My ComLuv Profile

  16. Sid 31. Jan, 2010 at 11:27 am #

    Thnks for your tip really usefull mate :)
    Sid´s last blog ..5 Most Effective Tricks to become a Successful Blogger My ComLuv Profile

  17. Jamie 03. Feb, 2010 at 12:10 pm #

    Thanks for this! Was nicely explained, alot of tutorials on the internet now seem to be hard to follow, so major well done! :)

Trackbacks/Pingbacks

  1. Tweets that mention How-to Wrap Google Adsense In Wordpress Posts Correctly | AEXT.NET -- Topsy.com - 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 [...]

  2. tripwire magazine | tripwire magazine - 21. Jan, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]

  3. 110+ Fresh Community Articles for Web Designers and Developers | tripwire magazine - 22. Jan, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]

  4. 110+ Fresh Community Articles for Web Designers and Developers | Afif Fattouh - Web Specialist - 23. Jan, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]

  5. 110+ Fresh Community Articles for Web Designers and Developers | Afif Fattouh - Web Specialist - 23. Jan, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]

  6. links for 2010-01-25 at DeStructUred Blog - 25. Jan, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly | AEXT.NET (tags: wordpress adsense google hacks articles tips) [...]

  7. How To Wrap Google Adsense In Wordpress Posts Correctly | Design Newz - 26. Jan, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]

  8. 10 wordpress hacks & tricks that I like at The MegaMag - 07. Feb, 2010

    [...] Source : aext.net [...]

  9. wp-popular.com » Blog Archive » How To Wrap Google Adsense In Wordpress Posts Correctly | AEXT.NET - 18. Feb, 2010

    [...] this article: How To Wrap Google Adsense In Wordpress Posts Correctly | AEXT.NET Tags: [...]

  10. Very Useful 65 Wordpress Hacks | Design your way - 08. Mar, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]

  11. Very Useful 65 Wordpress Hacks | Web Development News - 12. Mar, 2010

    [...] How To Wrap Google Adsense In Wordpress Posts Correctly [...]

Leave a Reply

CommentLuv Enabled