If you want to squeeze every last drop of revenue potential out of your blog without annoying your loyal visitors then you need to be a bit clever about the way you display advertising. I’m going to show you some simple PHP template code you can use to add some ad’ logic to your template.
First thing we would want to do is not show adsense advertising to registered users. I would still show CPM adverts as you need the page views to gain advertisers and get paid, and can slot those ads into areas where they will not be an annoyance.
Next you might want to show additional Google ad units for visitors arriving from search engines. As we have discussed before, these visitors are least likely to become regular readers and most likely to click your ads – two good reasons to at least try it, eh?
Most people will want to use channels for their adsense and chitika ads, also chitika allows you to use keywords. We can add these pieces of info dynamically. For the channels my example uses a scenario you might want to consider if you are building a multi author blog. In my case I want the bloggers who contribute to earn from their own work so I am going to use channels to determine their payment, the channel name is their own name and what the blog earns goes to them.
We should also look at how you can display different ads depending on the date, seeing as right now as I post this we are in the final throws of the Christmas season!
Lastly, for this post, we will perform some logic based on age of post.
I am going to use WordPress and Drupal for these examples but you should be able to translate to any PHP based blog with some tweaks. Let’s get on with the code.
Each blogging system has a set of template tags that provide bare minimum set of information that you can use for changing the display of your template at run time. If there is not the information you need already available then you need to start hacking code.
Take a look at the WordPress list and the Drupal PHPTemplate. As you can see they don’t provide much in the way of fancy information, just the basics. That is fine, just means we have to work a little if we want to do something really advanced.
Ad display code
The basic Chitika code is as follows
As you can see I have modified the default to provide variables for “channel” and “keyword”. We can provide the channel for Adsense in a similar fashion.
In Drupal to see if the person viewing your blog is logged in we can simply check the
$user->uid property. In WordPress we have a
If we want to show something or not in WordPress based on user status we can simply use something like the following.
echo “Logged in”;
echo “Not logged in”;
Here is the full Drupal code
uid < 1)
function showAd($channel, $keyword)
As you can see I have created a function for displaying Chitika which takes channel and keyword as parameters. I only call this function if the person is not a logged in user (user id would be less than 1).
While displaying the HTML this way is not the most efficient it does mean you can paste any HTML there instead of my Chitika code so you can replace banners or adsense if you wish.
So how about displaying ads only to visitors from search engines? To do this we simply use the PHP environment variable
Obviously you would need to check for Yahoo!, MSN, etc too.
Set the channel
We can use anything we like for the channel within reason, some people set the channel to where on the page the ad unit was displayed, sometimes based on topic etc. I set the channel to the author using
$channel = $node->name; in Drupal. In WordPress it would be
the_author('login',false) which says “return the login of the author of this post, do not output as html”.
Date Sensitive Ads
Around Christmas time you might want to push gift advertising, while in summer you might want to show vacation or “back to school” type products. All you have to do is check the month or date, for example..
If you want to only show advertising (or show more advertising) on old posts, you can work out the days since the post was created and determine what you want to do.
In Drupal we can work out days since post creation using
In WordPress the full code is
post_date)) / (60 * 60 * 24);
if ($days > 30)
echo “Posted $days days ago”;
This is just a taste of some of the things you can do. I will post a follow-up with more complicated code (such as database reading and writing) soon so do let me know if there are things you would like me to cover in part two.