If you'd like to change your site's appearance based on the current user's role, you can easily add the current user's role to the $body_classes array.
Just add this snippet to the yourTheme_preprocess_page function in your template.php file.
Drupal 6:
function grasmash_preprocess_page(&$vars, $hook) { $body_classes = array($vars['body_classes']); if ($vars['user']) { foreach($vars['user']->roles as $key => $role){ $role_class = 'role-' . str_replace(' ', '-', $role); $body_classes[] = $role_class; } } $vars['body_classes'] = implode(' ', $body_classes); // Concatenate with spaces }
Thanks to Anders Iversen for a Drupal 7 Version:
function grasmash_preprocess_page(&$vars) { if ($vars['user']) { foreach($vars['user']->roles as $key => $role){ $vars['class'][] = 'role-' . drupal_html_class($role); } } }
You may also want to check out my post on adding body classes based on user permissions.

Comments
Anders Iversen
Wed, 10/05/2011 - 18:09
Permalink
For Drupal 7
In drupal 7 things have changed a tiny bit.
You need to add it to the function ninesixty_preprocess_html instead:
function ninesixty_preprocess_html(&$vars) {
// ... there might be other stuff here ...
$body_classes = array($vars['classes_array']);
if ($vars['user']) {
foreach($vars['user']->roles as $key => $role){
$role_class = 'role-' . str_replace(' ', '-', $role);
$vars['classes_array'][] = $role_class;
}
}
}
Cheers :)
madmatter23
Wed, 07/18/2012 - 08:40
Permalink
Thanks Anders. I've updated
Thanks Anders. I've updated the article to include a Drupal 7 version.
Steve Kramer
Tue, 03/06/2012 - 00:27
Permalink
Worked Great
Anders Iversen's solution for Drupal 7 worked great. I'm using the zen theme though and I had to change "vars" to "variables" in all instances.
synthetic
Wed, 08/08/2012 - 22:22
Permalink
I could never get this to
I could never get this to fully work in my theme, and found a viable alternative is to use context 3. I simply created one context for each role, works great.
Condition: User role (Select role(s) for which to set the context)
Reaction: Theme HTML (text in section class field will be printed in body class)
One limitation is that the
Craig Tockman
Wed, 11/28/2012 - 11:42
Permalink
Contexts. This worked for me too
Same problem and same solution. Thanks.
Anonymous
Wed, 08/08/2012 - 22:26
Permalink
One limitation is that the
One limitation is that the section class field is static text, though it worked just fine for my limited amount of roles & needs.
Flavio
Fri, 03/01/2013 - 08:06
Permalink
Great!
Thank you very much. For Drupal 7 it works perfectly!
Remaye
Mon, 03/04/2013 - 03:35
Permalink
admin pages
Anders Iversen's solution works great except for all admin pages (pages /admin/*).
Any idea of a possible workaround ...? Thanks.
remaye
Mon, 03/04/2013 - 04:06
Permalink
admin pages
hum... sorry... of course it doesn't work : admin pages have a different theme !!
(shame on me)
Dee
Sat, 04/27/2013 - 18:59
Permalink
Unfortunately, it didn't work
Unfortunately, it didn't work for me in D7. I tried both the code in the article and comment. The roles names aren't showing up. I cleared cache, theme registry, everything. :(
Add new comment