Adding A Class To A Drupal Theme For Logged In Users
I was working on a Drupal theme recently which required a number of different elements to be present on a number of different pages, but only for logged in users.
Using a little PHP I was able to add a class of 'in' to the body which would only appear in the html when a user was logged in.
To do this, just find the opening <body> tag in your page.tpl.php file, and add in the code below.
<body<?php global $user; ?><?php if ($user->uid): ?> class="in"<?php endif; ?>>Then, you'll be able to write a general rule for logged out users, such as:
body {
background: #fff;
}And a more specific rule for logged in users, such as:
body.in {
background: #000;
}




Hi, I'm Laurence and this is my Drupal blog.
Don't Make Me Think!
Pro Drupal Development
PHP Cookbook
Will You Please Be Quiet, Please?
How simple can things be?
Great post!
Jan
Pretty cool. Anyone have any examples where this would be useful? I know that the Zen theme comes with this as well.
This seems like a slick and simple solution, but isn't there a potential security risk to making the $user variable global? I've seen this example in several other places, but no one has addressed the security angle.
do you know what i would need to do to allow loged in users the ability to see a "form" that has been written in html? It's a form that allows the user to log into there email address and right now i have to re direct the user to another site.
Hi Michael, where is the form exactly?
Post new comment