Gbp Central For Wordpress Support
Premium wordpress theme

I found a easy way to add adsense ads in posts or pages using functions code in functions.php that I found while surfing from this article the code is at bottom you need to add your adsense code where it says Add Adsense Code Here.

You can use a short code to position adsense any where on your post or page as this function will take of positioning your Adsense.

The code that needs to be added to posts and pages is shown below:
it is called in the text with:

[ adsense float = 'left ' ] this can be right or center No Space in [ ] space are added to display the shortcode.

// allowed parameter: 'left', 'center', 'right', 'none';
// other entries will be treated as 'none';
function add_adsense($atts) {
'float' => 'none', ), $atts)); if( $float == 'left' ) $float = ' style=" float:left; margin: 10px 10px 5px 0; "'; elseif( $float == 'center' ) $float = ' style=" display:block; margin: 10px auto; text-align:center; "'; elseif( $float == 'right' ) $float = ' style=" float:right; margin: 10px 0 5px 10px; "'; else $float = ''; $ads = '<span class="adsense "'.$float.'> Paste Your Adsense Code Here </span>'; return $ads; } add_shortcode('adsense', 'add_adsense');

Govindji Patel
I am Author/Publisher WordPress Coach and Consultant who enjoys writing tutorials and support and help wordpress users build better WordPress website/blog. Read Testimonials
Govindji Patel
Govindji Patel

14 Responses to Video:Add Adsense In Posts Using Functions In WordPress.3.0

  • Elvin says:

    Hi Govindji. I have been looking all over the internet but I can’t seem to find how to wrap by google adsense ads in my single.php file. I’m using the wordpress Twenty Eleven theme with coding which are different from all the adsense wrapping tutorial out there. Could you help me out? My full single.php codes are below. Thank you so much man.

    <?php previous_post_link( '%link', __( '← Previous’, ‘twentyeleven’ ) ); ?>
    <?php next_post_link( '%link', __( 'Next →’, ‘twentyeleven’ ) ); ?>

    • The file you looking for is content-single.php you need to add in that file same way I have on the post above yours but you need make change in child theme so copy the file and put in child theme and make change there.

      <div class=”entry-content”>
      <div style=”float:left; padding-top:20px; padding-left:10px;padding-right:10px;”>
      Adsense Code Here </div>


      <?php the_content(); ?>
      • Elvin @ dog crate covers guide says:

        Thank you so much Govindji! You’re a real life saver! I could never have guessed that the correct file was not single.php but rather content-single.php. As a little show of gratitude, I’ve google +1 your article :) God bless you!

    • If you want add directly in single.php you need add the adsense code just after

      <div class=”entry-content”>
      <div style=”float:left; padding-top:20px; padding-left:10px;padding-right:10px;”>
      Adsense Code Here </div>
      <?php the_content(); ?>

  • Toure says:

    Hello, is there a way to simply put a similar code inside the functions and forget about without messing any other files of the theme. I need something I can simply put in the functions.php and have it shows adsense below posts regardless of pages, articles or custom post.
    Thanks for any help on that.

  • alchymyth says:

    thanks for turning my article into a video tutorial – it proofs that you don’t need plugins for any minor tasks to customize your powered site.

  • onedaybox says:

    I don’t seem to have the option “editor” under the category “appearance” on y blog. I am using the template twenty ten by the wordpress team. Is there anyway I can add goodle adsense banners in the right hand column of my blog so the banner is below my categories for example?

    I have tried adding it using the widget text but that doesn’t work either….

    Thanks for your help!

    • Since you do not have a editor in Appearance  I assume that you are using for your blog and you may not have way to install a plugin either so you will be limited to adding adsense as I have done in this article.

  • maa says:

    Hi. thanks for the tip. This works. But google adsense technically only allows the adds to repeat 3 times. By using the codes that you suggested, does this mean the ads will appear in all 10 posts assuming the adsense float code is inserted once in 10 posts?

    • It will only appear where ever you put the short code when you write your post or page like I have done on this site.

      I have the put adsense short code in single.php so that it appears float to left on all articles and if I want another one I can put a short code in article when I publish manually.