Show/hide certain profile fields depending on user role or user permissions

Last modified: August 23, 2009 - 21:20

Description
This collection of snippets allows site administrators to show or hide specific profile fields, depending on user permissions or on user roles.

Usage

  • For use in a user profile page override
  • Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the snippet into the user_profile.tpl.php file
  • Tested and works with Drupal 4.5 and 4.6
  • Change the div class name or the link text to suit.

Show content based on user permissions

User permissions are specified on the ADMINISTER --> ACCESS CONTROL page.

<?php if (user_access('administer users')): ?>
<div class="fields">CONTENT FOR ADMIN ONLY GOES HERE</div>
<?php endif; ?>

Show content based on user role.

In this example snippet, the content will only be displayed to users with the super admin role type. Change the role type, DIV etc. to change the output.

<?php if (in_array('super admin',$GLOBALS['user']->roles)): ?>
<div class="fields">Super Administraator</div>
<?php endif; ?>

Show "Read User's Blog" on profile if user has permission to add blog entries.

In this example snippet a "Read User's Blog" will be added to a user profile if the user has the 'blog_author' role.

<?php if (in_array('blog_author',$user->roles)): ?>
<div class="fields"><a href="/blog/<? print $user->uid; ?>">Read <?php print $user->name ?>'s Blog</div>
<?php endif; ?>

 
 

Drupal is a registered trademark of Dries Buytaert.