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

Drupal: Using flexiblock to show/hide AdSense

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

By Khalid on 2005/10/06 - 15:05, last updated 2005/10/22 - 14:39

As the author of the Drupal adsense module, I often get queries on how to display ads in unusual ways.

Normally ads can be done using one of two ways:

Block Ads

if the ads are in blocks, then you just add  PHP blocks with the call to the adsense_display() function call. Ads in blocks can be restricted to certain pages, for example, you do not want them to display in node/*/edit and node/add/* pages because the Javascript based WYSIWYG editor you use is messed up by Google's Adsense code.

Content Area Ads

Displaying ads in the center content area requires  that you change the theme and place calls to the about function calls where you want them. There is no easy way to restrict their visibility like you do for block ads.

To solve this issue, and have more flexibility, we can use the flexiblock module.

Configuring flexiblock

Using flexiblock, you define a new block, make the format PHP, and paste the following code in it.

<?php
if (module_exist('adsense')) {
print adsense_display('336x280');
}
?>

Then using the standard block mechanism, you can make the block show or not show depending on the conditions you want, such as those mentioned above.

Next, using flexinode settings, make this block of type flexi, and assign it to a certain region (say 1 in the example below).

Modifying your template

Assuming you are using a phptemplate theme, you create a template.php file, and place  the following function in it.

<?php

function phptemplate_get_flexiblock_content($region) {
$blocks = theme('flexiblock_blocks', $region);
foreach ( $blocks as $block ) {
$content = $block['content'];
}
return $content;
}

?> 

The trick here is that I am able to display the block content only and not the subject, which makes the block look good in the content.

If you do want to show the block title, then you can use something like this snippet inside the foreach loop:

<h2 class="page-title"><?php print $block['subject'] ?></h2> 

The final step 

Then, in your theme, assuming it is phptemplate based, you place the following code. For example, if you want the block to show on every page, then you change page.tpl.php. If you want it to be in every node, then use node.tpl.php, ...etc.

<?php print phptemplate_get_flexiblock_content(1); ?>

The above can also be achieved using 4.7 regions feature, but with some more code.

Contents: 
Drupal

Book Navigation

  • ‹ Drupal Module: Google AdSense
  • up
  • Links and Resources on Google Adsense ›
  • Add comment

Comments

Rob (not verified)

Newbie question: hiding a block for a certain role

Sun, 2005/11/20 - 13:59

Thanks for this article! I'd been looking for a way to hide the title of a block beyond simply not titling it to begin with (since one can only have a single untitled block).

Could you tell me how I'd go about hiding a block for a certain user role? I have some blocks (namely ads) on my new site (www.bookmuncher.com) that I want to always show to most roles, but never to others.

  • reply

Khalid

Here is your answer

Sun, 2005/11/20 - 21:47
global $user;
$output = '';
$rolename = 'rolename';

if ( (is_array($user->roles) ) && (in_array($rolename, $user->roles))) {
$output = 'block content goes here';
}

return $output;

As per this page on Drupal.org : showing block content only to certain roles.

  • reply

Rob (not verified)

Thanks! :-)

Mon, 2005/11/21 - 00:27

I found that article shortly after I posted my question here. Thank you so much for your quick response. I was actually looking to be able to tell it what roles NOT to show the block for, but in the end, this will work the same way--I just list the roles that don't get to block the ads.
:-)

  • reply

Alexandros Roussos (not verified)

Adsense after first post only

Fri, 2006/06/02 - 06:48

Hi,

I'm trying to modify my PHPTemplate so as to display Adsense ads only after the first post.

However, the basic trick with the counter that resets itself that works on my wordpress blog template doesn't seem to work.

I thought that node.tpl was generated in a loop for each post. But my counter actually doesn't increment for each post, it remains 0, its probably because of the way PHPTemplate works.

Do you know any algorithm that can achieve is with a PHPTemplate archive? Can we do that with flexiblock?

Thanks in advance.

  • reply

Sergei (not verified)

Here is how you insert ads after certain post

Sun, 2008/02/10 - 17:54

Add this code into your node.tpl.php (I don't know how to preserve formatting of the code)

if(!isset($GLOBALS['g_PageIndex']))
$GLOBALS['g_PageIndex'] = 0;
if(1 == $GLOBALS['g_PageIndex'])
print "Hello, world!\n";
++$GLOBALS['g_PageIndex'];

  • reply

Rahim (not verified)

Can I show it in the content without flexiblock?

Tue, 2007/09/25 - 10:53

Flexiblock, as I know, is compatibel with only 4.x versions of Drupal. Is it possible to insert the ads into the content area in any other way? (for version 5.x and above?)

Thanks

  • reply

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.