As you know, Adsense ads perform best when they’re embeded in the main content. But, as you also know, the content of a WordPress blog is displayed by a tiny template tag, i.e. the_content(). So, theoretically, you don’t have many options when it comes to deciding where the ad will appear. Using the default code of a common WordPress theme, you can only place ads above or below the content of a single post.
In this article, I’ll show you how to display Adsense ads anywhere inside a post. I mean anywhere. How to control the ads’ position. How to display multiple ad units in particular posts and how to disable them in other ones. And especially I’ll show you how to automatically do all these things.
The common practice
The common practice (you can see it implemented on this blog) is to insert an ad above the content and display it inside through styling. The code will be:
<!–the Adsense code goes here–>
</div>
<?php the_content() ?>
(The style doesn’t have to be inline; you can also use a selector.)
This script will display an ad in the upper left area of a post. But what if you want to place the ad in the middle of the text? Or in the bottom area? And what if you want to place multiple ad units in posts that perform well in search rankings? Or to disable ads in the latest posts, so that your regular readers didn’t get annoyed? To be able to do all these things, you need to somehow get control over the content block that WordPress dynamically displays.
Using a shortcode
The shortcode solution consists in inserting a little piece of PHP (a function containing the Adsense code) in the functions.php file and calling the ad while creating a new post (or while editing an old one); to call the ad, you’ll use the “[adsense]” shortcode (without the double curly quotes) in the HTML mode of the text editor. The function was written by Jean-Baptise Jung:
return ‘<!–your Adsense code goes here–>
‘;
}
add_shortcode(‘adsense’, ‘showads’);
The shortcode will allow you to place one or two/three ad units anywhere inside the content. You can even align the ads within the text, using an inline style or a selector, for instance:
[adsense]
</div>
or
<div class="alignleft"><!–the theme you’re using should already have a style for this class–>
[adsense]
</div>
Tha main drawback of this solution is that you always have to remember to insert the shortcode when creating a new post. And this may be quite a challenge. I know that, ideally, one should be very careful before pressing the “Publish” button, read the text twice, select the proper keywords and so on, but in the real world things are a little bit different. If you published a few posts in a row and your attention is diminished, or if you’re tired or distracted, it’s probable to simply forget to perform all these repetitive tasks. Then you’d have to edit the post again and again (to asign it to a category, to add some tags, correct a misspell or insert a adsense shortcode. We’ve all been there. Editing a published post isn’t essentially a bad thing when moderately used, but the abuse has its dark sides: it is boring, it sends useless pings (and you’d run the risk to get penalized by some search engines), and it might republish the post in the RSS flux, which would probably irritate your regular readers. So, practically, you need a way to tell WordPress to remember you insert the Adsense shortcode in the posts you create.
Automatic shortcodes
One of the great things about WordPress is that you actually can talk to it. Of course, you need to speak its tongue, but you can be sure it will answer you back.
Applying this general principle to our matter, we can ask WordPress to automatically insert the Adsense shortcode in the text editor. To do this, we’ll adapt a filter written by Justin Tadlock:
function my_editor_content( $content ) {
$content = ‘<div style="float:left;margin-right:10px">[adsense]</div>’;
return $content;
}
NB When using Justin’s filter to preset some HTML, replace the double quoutes in the original code with single quotes; otherwise, you’ll get a syntax error.
Afther having added the code above to the theme’s functions, the text editor of your blog will look like this:

The full code
The full code you’ll have to add in the functions.php file is:
return ‘<!–your Adsense code goes here–>
‘;
}
add_shortcode(‘adsense’, ‘showads’);
add_filter( ‘default_content’, ‘my_editor_content’ );
function my_editor_content( $content ) {
$content = ‘<div style="float:left;margin-right:10px">[adsense]</div>’;
return $content;
}
Applications
By combining the two scripts by Jean-Baptiste Jung and by Justin Tadlock in the way I presented, you’ll get to:
- display and style Adsense ad units anywhere in the content (just write above and/or below the styled Adsense shortcode you’ll find in the text editor every time you create a new post);
- easily change the position and placement of an ad (you can float it to left or to right in any single post by only changing the inline declarations, e.g. “float:right;margin-left:10px”;
- disable ads in particulr posts by only deleting the automatically displayed shortcode;
- place differently styled ad units on selected posts (just copy/paste the automatic shortcode and change its style according to the ad unit’s context);
- benefit of the advantages offered by powerful Adsense related plugins without conflicting with the Adsense Program Policy (plugins are pieces of software, so, formally, you’re not allowed to paste Adsense code in their files; functions.php, on the other hand, behaves like a plugin but is definitely a template file).
Bonus
Using this model, you can get any shortcode automatically appear in the text editor of your blog.
Drawbacks
The solution i exposed has three drawbacks:
- you can use only one ad format;
- you can only use the HTML mode of the text editor;
- the automatically embeded code won’t work for older posts (you still have to manually edit them).
Other possibilities
Theoretically, it should be possible to automatically display ads only in posts older than x days, but the eventual filter would annulate the advantages listed above. I said “theoretically” because I got the idea while writing this article, so I couldn’t explore it. I hope I’ll find the time (and will have the skills) to do it.
Demo and download
You can see this solution implemented on my new demo blog – it is run by my mean boss
who publishes there his crazy ideas in automatic translation mode.
NB To see it, please select the Doc theme in the theme switcher.
I prepared two files for download. The first one is functions.php. If your blog theme doesn’t already have such a file (and, of course, if you want to have full control over the placement of the Adsense ads displayed on your blog), download the zip archive, extract the file and upload it in your blog theme directory.
The second file is functions.txt. If your blog theme already has a functions.php file, download this archive, extract the text file, copy its content and paste it in the existing functions file, just above the ?> signs.
NB Don’t forget to replace <!--your Adsense code goes here--> in the downloaded files with your Adsense code.
Cautions
Please don’t touch the first and the last line in the functions.php file, or your blog will magically disappear (I’m kidding, it won’t disappear, it will only display a white page).
Please don’t confuse the php and the txt files you might download here; you risk either to get nothing changing, or to get a fatal error.
All along this article, I assumed you knew the Adsense Program Policy (especially regarding the ad placements).
Finally, and most important thing, if you’re not familiar with PHP and with editing template files, you better ask someone to help you.
PS I take no credit for the solution I presented here. I only combined and played around with two existing pieces of code.
Leave a Comment on “Get full control over the placement of Adsense ads in single posts”
Some Pings and Trackbacks Are Welcome