I tried various methods of integrating Adsense code into my Drupal installations.
I used the ‘footer’ section in the ‘settings’ area of Drupal. This allows the display of adverts down the bottom of the page.
I also tried using blocks. Blocks were useful because they provide functionality for hiding themselves. You are not meant to show Google Adsense on pages like user registration.
These two approaches meant pasting Google Adsense code into each Drupal installation via its web interface. Google Adsense code doesn’t change often though, so it isn’t such a bad thing.
I also tried hacking around in the Drupal theme templates to place my Google Adsense where I wanted.
In the end I was using all three of these methods. Lately I had a look at it again and managed to find what I think is a nicer solution. It still isn’t perfect, but will do the job for what I want.
In my Drupal 4.6 installations, I now use a PHPTemplate the following way to place my Google Adsense.
I have a regular expression check at the top of the ‘page.tpl.php’ file for the Drupal theme I am modifying. This creates a boolean variable and contains the following code:
<php $url_path = drupal_get_path_alias($_GET['q']);
$regexp = '<^(?!admin|user/register|node/add|node/\d+/edit|node/\d+/outline)>';
$show_ad = preg_match($regexp, $url_path); ?>
Now, if I can modify the regular expression in one place and it will be taken into account for all places I use it for in this Drupal them template. Luckily for me, I was happy enough with the Google Adsense placings I was able to achieve using just the ‘page.tpl.php’ Drupal theme template file.
When I find the place I want to insert the Google Adsense code, I the place the following in the Drupal theme template file.
<?php if ($show_ad) { ?>
<div class="adsense-search">
<?php readfile($GLOBALS['base_url'] . "/inc/adsense_search.php") ?>
</div>
For each different type of Google Adsense advert I want to show, I create a new file with the Google Adsense code pasted in. In this example, the file is the Google Search feature and the Google Adsense code is placed in ‘adsense_search.php’ file. I also have files for Google Adsense Links and Adverts.
At some later point I could see myself wanting to add an option to turn these ads off on a per website basis, perhaps by adding another variable to my show_ads check. Given that I’m still learning Drupal and PHP, I’ll leave this for the moment. I’m not convinced this is the best way to include Google Adsense into a Drupal install, but it is pretty clean and works for me at the moment.
I had some problems with the Logitech MX Master 3S Mouse with an M1 MacBook Pro, but I had better luck with a Logitech G502 X Mouse