Is it possible to perform condition inside template.php (if logged_in, do these...else do that/ do nothing)?

I am a front-end developer, and as a front-end developer, i like my code clean. So i'm cleaning up my drupal html source code output.

I found some modules out there, that help a lot, and one of then is HTML5 Tools and Metatag (thank you for the team developers). But even with those great tools, i still having the "Drupal.settings" in my html output.

Ocasionally, i found that i can remove the "Drupal.settings" from the html with the hook_js_alter :

function MYTHEME_js_alter (&$js) { unset($js['settings']); }

But the problem is: my logged in users still needing this "Drupal.settings" because some modules use it. So that's why i'm asking if there is a way to perform condition inside template.php to logged_in/logged_out users.

THank you in advance

1 Answer 1


This should help you out. Drupal user login - How to tell if a Drupal user is logged in (authenticated)

You can also use the utility variable $logged_in: Returns TRUE if the user is registered and signed in.

Updated answer.

In template.php, you can use global $user; to check and see if someone is logged in.

global $user;    
if (isset($user->roles[1])) {
  //Put code here.

Checking against Devel, you can see that $user->roles[1] means it's an anonymous user. Output from Devel

  • It does NOT work with in "template.php". It only work for page.tpl.php
    – ottosatto
    Commented Jul 2, 2013 at 6:08
  • This should work for ya. If roles[1] is set, it means they're anonymous. Commented Jul 2, 2013 at 6:33
  • By the way, you have a new fan (lol) ;)
    – ottosatto
    Commented Jul 2, 2013 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.