Injecting Any Ads in Wordpress Posts Automatically
Dec 27 at 7:07pm by Andrew Benton
So, on one of my other sites (http://www.thatsingleguy.com), I wanted a way to automatically insert adsense (or any other ads source) into my posts without having to remember to use the adsense deluxe, select the correct one, and put the tag in ever single post I had written. Here is what I’m going to show you how to do:

At first I thought maybe altering the ‘the_content’ tag Wordpress uses to show an article was the way to go, but after being too lazy and too afraid to do that, I had a lightbulb moment. The answer was so clear I should feel dum, but I figure if I had this problem, countless others have as well. If nothing else, this can help the average blogger who isn’t great at altering themes, etc.
Step 1: Setup your ad
For my use, I decided based on the width of my content section, the 250×250 square ad was about as big as I figured my readers could stand. With AdSense Manager from http://www.mutube.com/, setup your ad as follows. I only wanted this ad to show up on single pages, and not on the main index template, but you can easily alter it to work on all pages, but be aware of the 3 AdSense limit thing.
- Setup your size, channels, etc.
- Make sure you have the Show Inline Ads settings like this:

Also be sure you make your HTML Markup section reflect where you want your ad (aligned right, left, etc).
Save that ad, and remember what you called it, mine was called In-Article. Once that is done:
- Go to Presentation > Theme Editor
- On the Right-Hand side, select Single Post
Find the line that has something similar to this:
<p>
<?php the_content(’<p>Read the rest of this entry »</p>’); ?>
Directly before the opening <p> tag, or if you don’t have a <p> tag, directly before the 2nd line, add the code for your adsense ad. Mine looks like this:
<?php adsensem_ad(’in-article’); ?>
And that should do it! Try it out. If you have any problems, please let me know, it may require some tweakage, but it should work for any wordpress blog, or any blog for that matter.









One Response for "Injecting Any Ads in Wordpress Posts Automatically"
Colin
January 3rd, 2008 at 7:15 pm
1Wow, I wondered how this was done. I’m going to try this out on my AmericaHugs Blog
RSS feed for comments on this post · TrackBack URI
Leave a reply