Community Documentation

Show block content only to certain roles

Last updated February 29, 2008. Created by jbrauer on September 27, 2006.
Edited by jmav. Log in to edit this page.

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;
}}
?>

Comments

Showing blocks on user profiles of particular role

Hello!

I am looking for a way to display a custom block on profiles of users that belong to a particular role. The block should be visible to anyone, but only displayed on profiles of users who have role x.

My searches returned no results.
Any hints in the right direction greatly appreciated.

PS
I am not a programmer, so plese ask for more specific info if needed.

Thank you!

About this page

Drupal version
Drupal 4.7.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.