Drupal Theming Tip: Show messages depending on whether the user is logged in or not.

Drupal logoI’m working on a Drupal theme at the minute where I need to work out whether a user is is logged in (or not) and show them a message accordingly. One such scenario is user invitations:

  1. I want to build in an invitation to become a member. If someone is already logged in, they don’t ned to see this message. If they’re not a user (or not logged in), I want them to see a message saying “Join our community” and providing a sign-up link.
  2. Certain pages have a wiki functionality. I want non-users to see an invitation to join up and edit, while existing members will see a message encouraging them to get involved. Slightly different messages, but both hopefully effective.

So, how does one do this in a Drupal theme? It’s actually quite easy.

For older versions of Drupal, they used to recommend the following code:

<?php
  global $user;
  if ($user->uid) {
    // User is logged in.
  }
?>

But in Drupal 6 there’s a tidy little function you can use: user_is_logged_in()

So, using that as the basis for a quick bit of code, you’ll have something like this:

<?php if (user_is_logged_in()) {
	//message to logged-in user
} else {
	//message to anonymous user
}
?>

Naturally, if you know a bit about PHP, you’ll be able to customise this to a number of different requirements. Hope this helps a few people out there in Drupal-land.

3 comments

  1. This helps, thank you. Any idea how I can use this to show a message saying “you are now logged in” at the top of the current page and then hide the message when the user visits a new page, or on refresh, or show it for a certain amount of time?

  2. Great!. Using this method we can display different headers for the authenticated users in our themes. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s