Community Documentation

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

Last updated August 23, 2009. Created by ztyx on January 21, 2006.
Edited by SLIU, bekasu, jbrauer, Dublin Drupaller. Log in to edit this page.

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

Comments

minor point

Don't have time to edit this page correctly, but, I just thought I would add a quick comment..i.e. for drupal 6.x, you would use $account rather than $user for some of the snippets above.

e.g. instead of

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

For Drupal 6.x you would use
<?php if (in_array('blog_author',$account->roles)): ?>
<div class="fields"><a href="/blog/<? print $account->uid; ?>">Read <?php print $account->name ?>'s Blog</div>
<?php endif; ?>

By uid for D6 would be like this??

<?php if (in_array('uid number',$account->uid)): ?>

As I'm not a coder this is all what I can think of...

I suppose the template name

I suppose the template name should be "user-profile.tpl.php" file instead of "user_profile.tpl.php"

Is there a way to hide

Is there a way to hide category and all fields listed in it ?

About this page

Drupal version
Drupal 4.5.x or older, Drupal 4.6.x, Drupal 4.7.x, Drupal 6.x

Theming Guide

Drupal’s online documentation is © 2000-2013 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. Comments on documentation pages are used to improve content and then deleted.