Blogging Tutorials

How to put Adsense Ads within the Content {WordPress}

Most of the times my readers and blogger friends ask about how to control the adsense ads placement inside the content of a post. Well, today let me describe it to you my way. What I use is a simple shortcode I wrote.


How to put Adsense Ads within the Content

First of all go to your functions.php (Appearance>>Edit>>Theme Functions(functions.php)) and put the code (the code below) at the end of it. After that click Update. (Do save a copy of your functions.php file before modifying it.)

 function the_adsense($atts) { return ' <div class="post-link-ad"> Put Your Adsense Code Here </div>'; } add_shortcode('ads1', 'the_adsense'); 

After adding this code all you have to do is to put [ads1] inside your post’s content (Visual or HTML works in both). The ad will show up where ever you placed the ad shortcode. Just like I added an ad below


Can I style it?

Yes go to style.css file  (Appearance>>Edit>>Style.css)At the end put this class. This step is optional.

 .post-link-ad{ text-align:center; /*It will make your ad displayed in center*/ background-color: white; /*Or anything you want*/ padding:5px; margin:5px; } 

Can I use two or more ads like this?

Yes of-course you can. All you have to is change the name of function and its shortcode e.g. another ad shortcode is below

 function new_adsense($atts) { return ' <div class="post-link-ad"> Put Your Adsense Code Here </div>'; } add_shortcode('ads2', 'new_adsense'); 

To put this inside the content you should add the shortcode [ads2] in Visual or HTML editor.

Your Turn:

  1. Tell me if you are still confused at any step.
  2. Did you find it useful? Are you planning to use it in a different way? Tell us!

By Freakify Editorial

The post was written by the Freakify Editorial Team. used to be managed by Awais, Maedah and their team before its acquisition.

38 replies on “How to put Adsense Ads within the Content {WordPress}”

It will speed up the process. you don’t need to worry about performance hen you click publish button it automatically pushes the ad’s text in the database.

Your Site Theme is simply awesome ,

Hey if we change the themes then we have to add it again ? but there is no problem in using Adsense insertion plugins ?

I Like Your Guide but I Am a little scared to use this method so i use a plugin called quick adsense which almost does the same thing buts its all automated great article btw :)

Why are you scared? I hate using 3rd Party plugins for such stuff. In this method all you get is a good deal of control in your hand. Let me know what is the hurdle ?

Why would you provide your adsense data to a third party plugin ? You ca make this handy little shortcode!
Thanks Mani for appreciating the site theme :) I coded & designed it (PS. Don’t forget to check the 404 Page).

Lastlty, yes the functions.php file is different in every theme. You will be in need to do it again if you change your theme.

I think plugin is best option for putting Adsense Ads within the Content. But it is nice to know the manual tricks

is there any way to add this[ads] code in all post???

because i have 1000+ post nd it take too much time to edit all post and add code in them??

This post is about getting control over the ad posting. You are again talking about automation :P
Just goto your single.php and post your static ad code if you want to automate it. Shortcodes work manually.

Thanks, that’s what I was looking for :-)

Is there a possibility to display the ads only in single posts? Let’s say I’d like to have 2 adsense block in each article, this way the home (or archive) in my case showing the 5 latest posts would display 10 adsense blocks which is not allowed :-( Hope you know what I mean?

Show only excerpts on your home page, if you are showing complete articles on home page then in these codes you can put a check,
Adsense Code here

If you didn’t get it let me know.

Good you took it fast!
Let me know if anymore is required. Keep visiting we of and on post good stuff.
Subscribe, to our Newsletter I bet you won’t regret.

Good tutorial Ahmad,
Actually it will look great having ads on different spots of each post, it will definitly maximise CTR cause readers will not be used to your ads spots.

But it will be difficult editing all published posts to include ads since its manual.

Now, after changing a theme, will I have to go to edit all published posts one by one or just add the codes to functions.php and ads will show on all posts having the shortcodes?

Again, how about customising the ads color to match the site color?

I will like to give it a try.

Great trick here Awais,
Just in a case if are not worrying about the placement of the ads, than by using the following code in your functions.php your ads will be injected in your every post

function ad_content($content) {
$postParas = explode(“”, $content);
$content = ”;
for ($i = 0; $i < count($postParas); $i++) {
if ($i == '2') { //I wanted to place ads after second para so in my case check is on second para,
$content .= ' Ad Code ‘;
$content .= $postParas[$i] . “”;
return $content;

add_filter(‘the_content’, ‘ad_content’);

The key point in this tutorial is reducing the automation and giving more control in hands of a user. By placing the ads on your own, you get more clicks more natural look of ads, and more revenue.

Yes! thats why I mentioned if you need. For instance if someone have hired writers to do the posting stuff, they can screw with html and whole look of the post, in such situations you will may not have the luxury to place ads by your self :)

I pasted the fucntion and then code in place
i am getting this error
function the_adsense($atts) { return ‘
‘; } add_shortcode(‘ads1’, ‘the_adsense’);
Warning: Cannot modify header information – headers already sent by (output started at /home/faraz/public_html/ in /home/faraz/public_html/ on line 876

Thanks for the tip. Although it is quite tiresome to put it in all of my existing posts, it was enough to get me started.

Comments are closed.