Using Drupal 4.7 regions to show/hide AdSense

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: 

Comments

RE: Drupal and adsense

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

Pages