Drupal: Using flexiblock to show/hide AdSense

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.

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.


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.



Newbie question: hiding a block for a certain role

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.

Thanks! :-)

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.

Adsense after first post only


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.

Here is how you insert ads after certain post

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

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

Can I show it in the content without flexiblock?

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?)