Last updated 19 March 2009. Created on 25 April 2006.
Edited by Rowanw, ajayg, add1sun, LeeHunter. Log in to edit this page.

Drupal 4.7 upwards comes with a new option to determine block visibility; by PHP code. When the code returns the value TRUE, the block is displayed, when the code returns FALSE, the block remains hidden:

  if (condition) {
    return TRUE;  // block will be shown
  return FALSE;

This is of course the verbose equivalent of:

  return (condition);

You can enter such code at the configuration page of each block in administer » blocks (admin/block). Configure the block and copy the code in the box below Page specific visibility settings. Make sure to select the last radio button (Show if the following PHP code returns TRUE).

For Drupal 4.6, you need to include the visibility conditions as part of the PHP code that generates the block.

Looking for support? Visit the forums, or join #drupal-support in IRC.


anawillem’s picture

i created a nodetype that is associated with a user's profile vis a vis the Content Profile module. i want to limit a block's visibility based on whether or not they have created that piece of content. so, for example, i have a nodetype called 'am_registration' which is associated to the user who created it. if they have already filled out this content type once, i do not want the block to show. because i only want this block to show up on one page, i have the code for it in the body section:

global $user;
if ($user->uid) {
  if ($user->am_registration) {}
<div id="register-button">
<p><h5>Congratulations! You are logged into the site!</h5></p><p><h4><a href="/node/add/am-registration">To proceed in your registration, please click here.</a></h4></p><p>&nbsp;</p><p>&nbsp;</p>

it works in terms of becoming invisible when the user is not logged in (if ($user->uid)) but the next embedded if statement doesn't seem to be doing a damn thing...



Oscar Illada’s picture

If any one have the solution, i dont want to show a block when i am paging, for ex in a url like this

CinemaSaville’s picture

I have the same need. Anybody know the solution? Thanks.

dirtysteak’s picture

Hi all, I have a views slideshow block that just won't behave on Internet Explorer, and I was hoping I could just put a custom PHP snippet in to hide this block if the visitor is using IE.

After lots of searching, I have not found an elegant way to pull this off. Would love to just contribute to the community, but I'm totally new to php.

Interested to see what the hive mind thinks...

walker2238’s picture

PHP is server side and therefor cannot tell the type of browser being used. You need to use javascript. There's lots of info around on how to block certain browsers. Google it of use one of the 10 or more modules made to block IE or some other browser.

Although the most elegant solution is to issue a support request for that module and have it become cross browser compatible.

baff’s picture

Here is a snippet which shows a block to a user who is flagged:

global $user;

$flag = flag_get_flag('ungelesene_km');

if ($flag->is_flagged($user->uid)) {
return TRUE;

return False;