Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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!