Last updated October 1, 2011. Created by pawel.traczynski on October 29, 2009.
Edited by JuliaKM, figaro. Log in to edit this page.
If you have a block that you want to set visible only for logged in users, follow these easy steps:
- Go to Administration -> Build -> Blocks page;
- Select "Configure" link next to the name of the block that you want to set visible only for logged in users;
- After the page reloads scroll down to the "Page specific visibility settings" field, select "Show if the following PHP code returns TRUE (PHP-mode, experts only)" option and paste the following code in the field "Pages" showed below the selection options:
<?php
global $user;
return (bool) $user->uid;
?>To display the block only for anonymous users use the following code:
<?php
global $user;
return !(bool) $user->uid;
?>Note: to use this method the PHP filter module must be enabled!
Comments
How to make this work for Drupal7
The "Show if the following PHP code returns TRUE (PHP-mode, experts only)" field exists in Drupal7, so I tried these steps anyway (even though above it says it's only for Drupal6), and it doesn't work.
Anyone know how to fix that? This seems like such a handy fix... but it doesn't work in Drupal7.
more testing
I found out this does work ONLY if you apply it to a block you've created. If you apply it to a default block, it gets ignored.
However, it's a default block that I NEED to change. (I want to set the content region to only be visible for authenticated users.) I also made other changes to default blocks and not ONE of those took. Only changes that I make to a block, which I've created, actually take effect.
That's a problem. Why?
Use Structure-Blocks
If you go to:
Structure
Blocks
Configure
Visibility settings
Roles
Show block for selected role, you can select the role that you want to see/not see the block.
This works for all blocks.
Hope this helps.
PHP code ingnored in panels
I created a block I want only anonymous user see. I used the above php code however when I use the block as a content in the panel, it simply ignores the PHP code. what should I do?