Defer Ads by Date on WordPress Post and Pages

Defer Ads by Date on WordPress Post and Pages

No Comments

The topic of advertising on a website can be a sensitive one. To some, it is intolerable under any circumstance, to others it’s just another part of the internet experience. I like to think I fall somewhere in between these two extremes. I realize that many site owners work very hard to create quality content and while some do this purely as a labor of love, it is still nice to be able to recoupe some of the time and expense of running their website.

Internet advertising can be something of a mixed bag for the visitor as well, but there is no reason a site can’t feature advertising that is safe, inoffensive and that at least attempts to be related to the topic at hand (such as Google’s Adsense).

The code snippet below attempts to strike a balance between the need for ad revenue and the user experience. It allows the site owner to differ ads from appearing in their WordPress site until after a number of days have passed. This way, faithful readers enjoy an ad-free experience for a period before the any single page or post becomes monetized.

This goes in any of the template files for your theme (single.php, page.php, etc) wherever you want the ad to appear. You can use it multiple times, and change the contents or different ad code.  You can further customize the appearing by applying CSS styling to the contents of the time deferring material.

[code]

GOOGLE ADSENSE CODE HERE


[/code]

This next part goes into your THEME’S functions.php file (NOT WordPress core functions.php). The $days = 60; can be set to whatever you like. Two months seemed fair to me.

[code] // ===================================
// Allow advertising on old posts only
// ===================================

function is_old_post($post_id=null){
$days = 60;
global $wp_query;
if(is_single() || is_page()) {
if(!$post_id) {
$post_id = $wp_query->post->ID;
}
$current_date = time();
$offset = $days *60*60*24;
$post_id = get_post($post_id);
$post_date = mysql2date(‘U’,$post_id->post_date);
$cunning_math = $post_date + $offset;
$test = $current_date – $cunning_math;
if($test > 0){
$return = true;
}else{
$return = false;
}
}else{
$return = false;
}
return $return;
}
[/code]

* Standard disclaimer here. Before editing any critical files in WordPress you should make a backup and be prepared to roll back any changes in case you run into problems or conflicts. This code is offered with no guarantees or support. Use at your own discretion.

Need help implementing this tip in your WordPress site? Contact us for our professional services.  We can also provide support & expertise in convenient “blocks” to suit your short and long term needs.

I'm the front-man of It's WordPress. I come from a diverse array of backgrounds, enjoying the opportunity to expand my knowledge base and skill set by re-inventing myself. I enjoy environments that focus on emerging information, technology and concepts. I put on the technical hat in my early 20s and never really looked back. I'm love technology and the internet, as well as the outdoors and avidly hike, kayak and camp every chance I get.

About Us

We can take you from concept, through design, development and deployment in one seamless process. Whether you choose a self-managed web site or need a continuing support relationship; we've got you covered.

CLICK TO EDIT

Request Consulation

Ready to transform your vision into a reality? Just looking to see what it takes to get the ball rolling. Tell us about your project and we can help. No spam. No obligation. Just answers.

WordPress Workflow With Rest API

Over the last decade we've seen the accelerating rise of dynamic JavaScript…
Continue reading

Three Must-Haves For Your WordPress Headlines

Simple headline best practices you can't afford to overlook. Don't bury the…
Continue reading

Hail to the King (of Content Management Systems)!

WordPress continues to grow as the CMS (Content Management System) of choice on…
Continue reading

Theme View – Cool Free Themes For WordPress Sites

In this post we look at three free themes that use the…
Continue reading

A Fist Full of Facebook… Plugins

Love or hate it, Facebook is still the 800 pound gorilla in…
Continue reading

Modifying the WordPress Admin Toolbar

The Admin Toolbar was introduced back in WordPress 3.1 (trivia points if…
Continue reading

Theme View – Cool Free Themes For WordPress Sites

In this Theme View update we feature themes that take advantage of…
Continue reading

Plugin Picks – Stock Image Finders for WordPress

A lot of fans, including myself, of the popular photography finder and…
Continue reading

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top