Show block content only to certain roles
To show a block to a one or more roles, put this code inside the block under Page specific visibility settings
Select -> Show if the following PHP code returns TRUE (PHP-mode, experts only).
Code
<?php
global $user;
$approved_roles = array('board', 'sales', 'engineer');
if (is_array($user->roles)) {
if (count(array_intersect($user->roles, $approved_roles)) > 0) {
return TRUE;
} else {
return FALSE;
}}
?>Users with the role 'board', 'sales' or 'engineer' will see block.
Sometimes you may want to hide a block for a certain role and show it to all others.
<?php
global $user;
$approved_roles = array('board', 'sales', 'engineer');
if (is_array($user->roles)) {
if (count(array_intersect($user->roles, $approved_roles)) > 0) {
return FALSE;
} else {
return TRUE;
}}
?>