Drupal: Add current user's role to body classes

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:

<?php
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:

<?php
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.

Drupal Version Compatibility: 

Comments

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 :)

Thanks Anders. I've updated the article to include a Drupal 7 version.

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.

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

Same problem and same solution. Thanks.

This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
<a href="http://www.cgo-media.com/seo-manchester/">SEO Manchester</a>

One limitation is that the section class field is static text, though it worked just fine for my limited amount of roles & needs.

Thank you very much. For Drupal 7 it works perfectly!

Anders Iversen's solution works great except for all admin pages (pages /admin/*).
Any idea of a possible workaround ...? Thanks.

Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
<a href="http://www.britanniasecuritygroup.com/">Roller Shutters</a>

hum... sorry... of course it doesn't work : admin pages have a different theme !!
(shame on me)

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. :(

This code will work with the Omega theme (which uses attributes_array['class'][] instead of classes_array[] :

if ($vars['user']) { foreach($vars['user']->roles as $key => $role){ $vars['attributes_array']['class'][] = 'role-' . drupal_html_class($role); } }

Try this one (it helped me):

function THEMENAME_preprocess_html(&$vars) {
foreach($vars['user']->roles as $role){
$vars['classes_array'][] = 'role-' . drupal_html_class($role);
}
}

---
Source: https://drupal.org/node/1489460#comment-8632639

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.