Skip to main content
Home
The Baheyeldin Dynasty
The journey for wisdom starts with knowledge
  • Home
  • About
  • Site Map
  • Contact

Using Drupal 4.7 regions to show/hide AdSense

  1. Home
  2. Resources for using Google Adsense with Drupal

By Khalid on 2005/10/08 - 13:09, last updated 2005/10/08 - 13:44

In a previous article, I described how one would use flexinode to display ads anywhere on the page, and not just blocks. This can be used to achieve things like restricting ads from showing on certain pages.

This can be achieved using Drupal 4.7 regions feature, which, like flexinode, allows multiple regions for blocks to be placed in. The default regions are left sidebar, right sidebar, header, footer and content.

However, blocks are always displayed with their title, which is ugly anywhere apart from the sidebars. Although CSS can be used to hide the header, this is more of a hack than the proper way of doing things.

Initial Attempts

Initially, I tried the following code in page.tpl.php:

<?php
$block = block_block('view', 1); // Block 1
drupal_set_content('content', $block['content']);
?>

The problem is that it will not obey the visibility rules of the block. Even if the block is disabled, it will still show in the following code. If it is to be shown only on non-admin pages, this rule will be ignored, and it will show everywhere. Not exactly what I wanted it to do.

The Proper Way

After corresponding with Nedjo Rogers, the author of 4.7's regions feature, and a lot of experimentation, here is the solution that can be used. This assumes that you have a PHPTemplate based theme.

Change the theme's block.tpl.php so that it looks like so:

<?php
switch($block->region) {
case 'ad_top':
case 'ad_bottom': ?>
<div class="<?php print $block->region; ?>"><?php print $block->content; ?></div>
<?php
break;
default: ?>
<div class="block block-<?php print $block->module;?>"
id="block-<?php print $block->module;?>-<?php print $block->delta;?>">
<h2 class="title"><?php print $block->subject; ?></h2>
<div class="content"><?php print $block->content; ?></div>
</div>
<?php
break;
} ?> 

Notice that for the ad_top and ad_bottom regions, only the block's content is displayed, and the region is used as the div's class.

Create a template.php or edit it, and add the following function to it:

<?php
function bluemarine_regions() {
return array(
'ad_top' => t('ad top'),
'ad_bottom' => t('ad bottom')
);
} 

This defines two new regions for your theme, ad top and ad bottom. You can add as many regions as you want.

Create new blocks, put in them the call to the adsense_display() function with the arguments you want, and assign them to the regions you created.

Now, you can use simple statements to display the ads in your page.tpl.php or node.tpl.php anywhere you want, for example:

<?php print $ad_top; ?>
<?php print $ad_bottom; ?> 

Although the technique I mentioned uses Adsense as an example, it can be used to display anything. For example, you can use code snippets from the Drupal web site and display them anywhere you want.

Contents: 
Drupal

Book Navigation

  • ‹ Links and Resources on Google Adsense
  • up
  • Using Google AdSense with Drupal ›
  • Add comment

Comments

AoC Gold (not verified)

RE: Drupal and adsense

Thu, 2008/08/28 - 07:38

Hi,

I use code snippets from the Drupal web site and display adsense and kontera also as is allowed by google, so you have 2 source of incomes. Very easy to implement as you commented.

Cheers,

Andy Colleman

  • reply

Pages

  • « first
  • ‹ previous
  • 1
  • 2

Current

Pandemic

  • COVID-19
  • Coronavirus

Search

Site map

Contents

  • Family
    • Khalid
    • Ancestry
    • Extended
  • Friends
  • Nokat نكت
  • Writings
    • Cooking
    • Culture
    • Science
    • History
    • Linguistics
    • Media
    • Literature
    • Politics
    • Humor
    • Terrorism
    • Business
    • Philosophy
    • Religion
    • Children
  • Technology
    • Linux
    • Arabization
    • Drupal
      • Association
    • Software
    • Internet
    • Technology in Society
    • Digital Archeology
    • NCR History
    • MidEast Internet
    • Programming
    • Saudi ISPs
    • Miscellaneous
  • Places
    • Canada
      • Weather
    • Egypt
      • Cuisine
      • Alexandria
      • E.G.C.
    • USA
    • Saudi Arabia
  • Interests
    • Astronomy
    • Fishing
    • Photography
    • Snorkeling
    • Nature
    • Photomicroscopy
  • Miscellany

In Depth

  • al-Hakim bi Amr Allah: Fatimid Caliph of Egypt الحاكم بأمر الله
  • Alexandria, Egypt
  • Arabic on the Internet
  • Articles on the history of Muslims and Arabs in the Iberian Peninsula تاريخ المسلمين و العرب في الأند
  • DIY GOTO Telescope Controller With Autoguiding and Periodic Error Correction
  • E.G.C. English Girls College in Alexandria, Egypt
  • Egyptian Cuisine, Food and Recipes مأكولات مصرية
  • George Saliba: Seeking the Origins of Modern Science?
  • Internet Scams and Fraud
  • Mistaken for an Arab or Muslim: Absurdities of being a victim in the War on Terror
  • Mistaken Identity: How some people confuse my site for others
  • One People's Terrorist Is Another People's Freedom Fighter
  • Overview of Google's Technologies
  • Photomicroscopy
  • Pseudoscience: Lots of it around ...
  • Resources for using Google Adsense with Drupal
    • Drupal Module: Google AdSense
    • Drupal: Using flexiblock to show/hide AdSense
    • Links and Resources on Google Adsense
    • Using Drupal 4.7 regions to show/hide AdSense
    • Using Google AdSense with Drupal
    • Drupal AdSense Module: Added Section Targeting
    • How To Include Drupal Comments In Section Targeting
  • Rockwood Conservation Area, Southern Ontario
  • Selected Symbolic Novels And Movies
  • Snorkeling the Red Sea near Jeddah
  • Updates and Thoughts on the Egyptian Revolution of 2011

Recent Content

Most recent articles on the site.

  • Origin Of COVID-19: Natural Spillover, Lab Leak Or Biological Weapon?
  • Kamal Salibi and the "Israel from Yemen" theory
  • How To Upgrade HomeAssistant Core In A Python Venv Using uv
  • Ancestry - Paternal Side
  • Review of Wait Water Saver For Whole House Humidifiers
more

Most Comments

Most commented on articles ...

  • Another scam via Craigslist: offering more than asking price
  • Warning to female tourists thinking of marrying Egyptians
  • Craigslist classified for used car: Cheque fraud scam
  • Winning the lottery scam email: World Cup South African lottery
  • Email Scam: BMW 5 Series car and lottery winning
more

About Khalid

Various little bits of information ...

  • Khalid Baheyeldin: brief biography
  • Presentations and Talks
  • Youtube Videos
  • GitHub Projects
  • Drupal.org Profile
  • Astrophotography @ Flickr

Sponsored Links

Your Link Ad Here

Tags

Android Mobile Ubuntu Sony OnStep OpenWRT Router Ericsson COVID-19 Rogers Coronavirus Arabic Kubuntu Home Assistant GSM Telescope tablet Spectrum Scam Python 419 Laptop Firefox DIY CPU Conspiracy Comet Balkanization backup App
More

© Copyright 1999-2025 The Baheyeldin Dynasty. All rights reserved.
You can use our content under the Terms of Use.
Please read our privacy policy before you post any information on this site.
All posted articles and comments are copyright by their owner, and reflect their own views and opinions, which may not necessarily be consistent with the views and opinions of the owners of The Baheyeldin Dynasty.

Web site developed by 2bits.com Inc.