5 Most Useful Adsense Hacks for WordPress

Share this post:

WordPress is the platform of choice for most bloggers, and WordPress hacks is one of the most popular topics.

In this article, I’m going to share some of the useful WordPress Hacks to incorporate adsense into your theme.

1. Place Ads on old Posts only

It will show the ads on posts that are older then 15 days. It is a great snippet to hide ads from normal visitors or subscribers.

Add the following code in functions.php file

function is_old_post($post_id=null){
   $days = 15;
   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;
}

After that you’re ready to call the functions in the single.php file as shown below .

<?php if(is_old_post()){ ?> INSERT AD CODE HERE <?php } ?>

Source Catswhocode

2. Display Adsense to Search engine visitors only

A simple but useful snippet to show ads to search engine visitors only.

function scratch99_fromasearchengine(){ $ref = $_SERVER['HTTP_REFERER']; $SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.'); foreach ($SE as $source) { if (strpos($ref,$source)!==false) return true; } return false; }

Then, paste the following code where you’d like to show the adsense ads

if (function_exists('scratch99_fromasearchengine')) {
  if (scratch99_fromasearchengine()) {
    INSERT YOUR CODE HERE
  }
}

Source Sratch99

3. Display Ads after the first post

Add the following code in index.php to display google adsense after the first post :

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); $loopcounter++; ?>

// the loop stuffs

<?php if ($loopcounter <= 1) { include (TEMPLATEPATH . '/ad.php'); } ?>

<?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

Source WebDesignerwall

4. Insert Google Adsense Anywhere in Your Blog Post

Add the following code in the functions.php file & make sure to add your adsense code

function adsenseads() {
    return '<div id="adsenseads">Put your Google Adsense banner code here</div>';
}
add_shortcode('showmyads', 'adsenseads');

After that use the shortcode in posts & pages to display the adsense ads

[showmyads]

Source WebDesignzo

5. Hide Adsense Ads to logged in users

Add the following at the time of inserting adsense ads
<code><?php if(!is_user_logged_in()) { ?> // Adsense Code <?php } ?>
About the Author: Devesh Sharma is a WordPress fanatic and loves experimenting with WordPress themes & plugins.Get more from Devesh on  and Twitter.
4 comments… add one
  1. Ming Jong Tey says

    Woh.

    This is kinda techi for me. However, I definitely agree it is great to have Ads displayed to search engine visitors only!

    Cheers,
    Ming

  2. online Strategies says

    Thank for these great hacks. They are simple hacks helps the blogger to display ads in the right place and at the right time.

Leave a Reply