Mobile Perimeter

Mobile Perimeter

Guard your data against loss with The Backup App.  Scheduled and on demand backups online, to SD card. Sync your data to a new phone without the prying eyes at your local service provider. Current Version: unreleased Release Schedule: 1.0 … More

Advertwhirl

Advertwhirl

Advertwhirl(pronounced Adver-Twirl) is the ultimate WordPress plugin to manage advertising campaigns for your site.  Advertwhirl not only allows you to manage exactly how and when ads are displayed on your site but allows you to serve your ads to external … More

Sentient Events

Sentient Events

Find local entertainment listings, buy tickets, review and rate events all from a free app for your Android based Smartphone.  Tickets can be be purchased through Fandango and Stubhub. Get event times and listings how you want them.  Want to … More

Automatic Insertion of Advertisements in WordPress Posts
5 posted by maxj | May. 01, 2011

Automatic Insertion of Advertisements in WordPress Posts

    Sections
  1. The Situation
  2. The Solution
  3. The Nuts and Bolts
    1. Using the Built-in Theme Editor
    2. Find the_content()
    3. Beginners method
    4. Advanced method
  4. Download Advertwhirl
  5. References
3 months FREE Hosting with FREE Domain Name

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

Download Advertwhirl

References

Mobile Sentience Advertwhirl page
Advertwhirl in the WordPress gallery
Tutorial with instructions for installing Advertwhirl Plugin
Original advanced method code

VN:F [1.9.8_1114]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.8_1114]
Rating: +1 (from 1 vote)

Automatic Insertion of Advertisements in WordPress Posts, 10.0 out of 10 based on 1 rating

Post to Twitter

5 Responses to Automatic Insertion of Advertisements in WordPress Posts

  1. math games says:

    hi

    VA:F [1.9.8_1114]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.8_1114]
    Rating: 0 (from 0 votes)
  2. Pingback: Simple Two Affiliate Ad Rotation | Mobile Sentience

  3. Pingback: Sharing Ad Slots With a Guest Blogger | Mobile Sentience

  4. Pingback: Even Easier Automatic Insertion of Advertisements in WordPress Posts | Mobile Sentience

  5. Pingback: Advertising Campaign Template - Best Affiliate-Best Affiliate Marketing – Best Affiliate-Best Affiliate Marketing

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Market Apps

  • Mobile Sentience has no apps in the Marketplace at this time.

Log In