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:

function grasmash_preprocess_page(&$vars, $hook) {
$body_classes = array($vars['body_classes']);
   if (
$vars['user']) {
$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']) {
$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: 


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.

Debt management and also credit counseling have become popular in the us today. With the bank card crisis rising, the credit counseling services are getting to be indispensable. <b><a href="http://www.adviceaboutfinance.net/category/credit-counseling">Credit Counseling<a/></b> </br> </br>
In the today's world, many people are getting through financial problems and as a result of various reasons cannot follow their budget. In that situation, credit counselling programs will help them create an original spending plan dependant on their needs. <b><a href="http://www.rainbow-finance.net/category/credit-counseling">Credit Counseling<a/></b> </br> </br>
Financial analysis can help determine the worth of one's business. Outsourcing financial analysis can assist you to get competent financial experts that will make recommendations after experiencing complex data and also financial statements. <b><a href="http://www.vipcarfinance.net/category/financial-analysis">Financial Analysis<a/></b> </br> </br>

Debt management and also credit counseling have become popular in the us today. With the bank card crisis rising, the credit counseling services are getting to be indispensable.

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>

There is no question that instantaneous water heaters are good options to trust in terms of their water heating services. However, you need to know that not all kinds of this water heater will work best for you.

I am really impressed by this excellent stuff on ZBrush which is a digital sculpting and painting program that has revolutionized the industry. I've enjoyed reading the nice post
<a href="http://www.costapharmacy.com/buy-valium/">buy valium online</a>

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

This is what I have been searching in many websites and I finally found it here. Amazing article. I am so impressed. Could never think of such a thing is possible with it...I think you have a great knowledge especially while dealings with such subjects.
<a href="http://www.1millionfans.com/">Buy Facebook Fans</a>

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

Good post you have shared here Thanks a lot.
<a href="http://www.stanleyhighschool.com">High School Diploma Online</a>

In order to succeed you must fail, so that you know what not to do the next time. <a href="http://www.bebenou.ro/">imbracaminte bebelusi</a>

Packers and Movers in Mumbai or http://www.expert5th.in/packers-and-movers-mumbai/

Packers and Movers in Hyderabad or http://www.expert5th.in/packers-and-movers-hyderabad/

Oh, this is a magical post. It’s life the most majestic post there is on this planet right now.

Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic. -

This article is really fantastic and thanks for sharing the valuable post.

Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic. -

Add new comment

Plain text

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