Various Ways To Display Adsense Ads on Drupal 4.7

Over the last year, as Drupal developed, and the Google Adsense module grew with it, I have posted several ways on how to display ads on a site. One of them use the flexiblock module, the other uses the new 4.7 "regions" feature.

Today, I am going to describe two easy ways of displaying ads on your site, one that does not require any coding, and another that requires a little bit of that, but does use the new built in blocks of the adsense module.

Using The Default Blocks

The easiest way to use the latest 4.7 adsense module, is to use one or more of the 5 pre-defined blocks. Just visit administer -> blocks and you will find these blocks titled "Adsense: unconfigured".

Select an ad format from the drop down list

Select a group.

Select a channel.

You can ignore those last two if you do not want to use them or if you do not know what they mean.

You can then enable the block, and assign it to a region like you do with any other block.

You can also specify which pages the block will be visible on if you want.

Using Your Theme

This method requires some basic PHP knowledge, and assumes that your theme uses the PHPTemplate engine.

If you want more control on placement of ads, you can still use the above technique to define your ad blocks, but leave the display to your theme.

Do NOT enable the block, or assign it to a region.

You then create or edit the template.php file of your theme. This should be in the same directory as the page.tpl.php file of your theme.

Add the following function:

function phptemplate_adsense_block($delta) {
$block = adsense_block('view', $delta);
return $block['content'];
}

You can now call that function from any of the tpl.php files, using the following code:

<?php print phptemplate_adsense_block(3); ?> 

Replace 3 above with the adsense block that you created earlier. This will be the number that you see on the status bar when you move the mouse over the configure link of the block, for example, if this is: http://example.com/admin/block/configure/adsense/2, then use 2.

Using this method, you can change the color of the ad or the channel without having to edit the theme files.

Other ways

Remember that you can still use the adsense tags as described in the help page for the adsense module, as well as direct calls to the adsense_display() function in any place where PHP code is allowed.

Contents: