Mobile Sentience has no apps in the Marketplace at this time.
Automatic Insertion of Advertisements in WordPress Posts
The Situation
You’ve been adding advertisements to your blog posts with a short code using the built-in editor ie… . It was a simple solution to get started and didn’t require any editing of your theme templates. But, you’ve decided the best way to monetize your blog is to put ads in every post and remembering to insert the short code sometimes just doesn’t happen.
If you’re having problems getting your first campaign setup check out this primer onaCampaigns, Allocations and Rules.
The Solution
Modify your theme’s single.php template file to easily insert an ad campaigns ads automatically in each post. Use allocation rules to determine which ads get inserted into which post while insuring that every post gets an appropriate ad and maintaining the consistency of your advertising campaign.
The Nuts and Bolts
While the modification of a themes template files isn’t for the faint of heart and is generally reserved for advanced users inserting and Advertwhirl ad into your post automatically is a pretty straightforward endeavor that can be accomplished by most anyone if they only have a little confidence.
Using the Built-in Theme Editor
WordPress has a theme editor that lets you edit your themes template files right through your blogs admin control panel. To get to the theme editor go to the “Appearance” menu and select “Editor”
Once you have the theme editor open go to the upper right hand corner and select the theme you want to edit. This should match the theme you are using for your WordPress site. For this demo we will be editing the default “Twenty Ten” theme.
Right below where you selected your theme you will find a list of templates for the theme. Click on single.php, this is the template file for single posts.
Find the_content()
Time to put on your brave face and do a little poking around. You are looking for a line that reads “the_content()” in many themes you will find it directly in single.php. In some, such as “Twenty Ten” single.php will load another template part that contains “the_content”.
Here is the line you are looking for
<?php the_content(); ?>
If your template for posts is loading a template part which contains the_content you will find a line that has “get_template_part(‘some_value’, ‘some_other_value’);” and you won’t find a line with “the_content();”. If single.php does have a line that says “the_content();” and also “get_template_part(‘some_value’, ‘some_other_value’);” don’t worry about the get_template_part line, you can just ignore it. If you don’t find “the_content()” you will want to open the template part. The template part you want to open will be composed of the two parts in get_template_part.
<?php get_template_part('loop', 'single') ?>
In the “Twenty Ten” theme single.php loads a template part named loop-single.php
If your theme has “the_content();” you are ready to go, if not open the template part. In the case of the “Twenty Ten” theme you want to open loop-single.php.
Once you find the content you can insert the ad campaign at the beginning or end of the post content. Or if you are a particularly brave soul you can insert the add in the middle of the content.
Insert advertisement before or after post content – Beginners method
The easy method is to insert the “advertwhirl_print_ad();” function before or after “the_content();” The example below will add an advertisement from the “Your_Campaign” ad campaign before and after the post content. You will want to change “Your_Campaign” to match the name of your campaign. If you don’t want to immediatly print the ad to the visitors browser but need to do something more with it first you can save the ad to a string with “advertwhirl_get_ad()”
<?php
advertwhirl_print_ad('Your_Campaign'); // This advertisement will be before your post content
the_content();
advertwhirl_print_ad('Your_Campaign'); // This ad will be after your post content
?>
<?php
$ad = advertwhirl_get_ad('Your_Campaign'); // The content of the advertisement will be saved to $ad as a string
$ad = str_replace($what_you_are_looking_for, $what_you_want_it_to_be, $ad); // Change the ad content somehow, prehaps customize it for the visitor
$echo $ad; // This will actually place the customized ad
the_content();
echo advertwhirl_get_ad("Your_Campaign"); // This is exactly the same as advertwhirl_print_ad('Your_Campaign'); and placed the ad after your post content
?>
Insert advertisement in the middle of your post content – Advanced method
The advanced method really isn’t so difficult, it just has a few more steps then the beginners method. The code that Mobile Sentience is using to insert ads in the middle of its posts is a variation of the code found in this excellent post at zedomax.biz.
The first step is to make sure you place all the code between the <?php and ?> tags that are surrounding the_content();. If your theme has them all on one line break them up now, also comment out the_comment(); command while you are at it by putting // before it. Make this
<?php the_content(); ?>
look like this
<?php //the_content(); ?>
Now add the code to insert your advertisement in the middle of the post content.
This is what you want to end up with
<?php
//the_content();
$adsEvery = 4; // How many blocks between each of your ads. The smaller the number the more ads, the larger the fewer
$maxAds = 2; // The maximum number of ads to put in a post. There is no maximum if $maxAds is -1
$content = apply_filters('the_content', $post->post_content); //get the post content store in $content
$paragraphs = explode("</p>", $content); //Separate the content into blocks
echo '<div>';
$blockCount = 0; //this is count for number of blocks
$adCount = 0; //this is a variable to keep track of how many ads have been put in the post
foreach($paragraphs as $paragraph) {
if(preg_match('/<p>/',$paragraph) == 0 && $blockCount % $adsEvery == 0 && ($adCount < $maxAds || $maxAds == -1)){
$float = $adCount % 2 == 0?"float:left":"float:right";
echo ' <div style="width: 300px; height: 250px; padding: 9px 9px 9px 9px;">';
advertwhirl_print_ad('Manifesto'); // This is what places the Advertwhirl advertisement
echo '</div>';
$adCount++;
}
echo $paragraph; //print the block
echo '</div>';
$blockCount++;
}
echo '</div>';
?>
Thats all it takes. If you want the two lines immediately following //the_content(); can be adjusted to configure how your ads appear.
“$adsEvery” controls how many paragraphs of content are between each ad. The default is to show one ad every 4 paragraphs.
$adsEvery = 4;
“$maxAds” lets you limit how many ads will be displayed in anyone post so you don’t overwhelm your readers. Ads lose potency the further down the post they appear and to many can annoy readers. The default is to show a maximum of 2 ads. If you don’t want to limit the number of ads that will be shown you can set $maxAds to -1.
$maxAds = 2;
Download Advertwhirl
References
Mobile Sentience Advertwhirl page
Advertwhirl in the WordPress gallery
Tutorial with instructions for installing Advertwhirl Plugin
Original advanced method code






hi
Pingback: Simple Two Affiliate Ad Rotation | Mobile Sentience
Pingback: Sharing Ad Slots With a Guest Blogger | Mobile Sentience
Pingback: Even Easier Automatic Insertion of Advertisements in WordPress Posts | Mobile Sentience
Pingback: Advertising Campaign Template - Best Affiliate-Best Affiliate Marketing – Best Affiliate-Best Affiliate Marketing