Profile picture for user admin
Daniel Sipos
12 Apr 2013

Ever needed to redirect a user to a custom page after logging in? Look no more as there are a couple of cool modules out there that allow you to do just that. Moreover, there is also some code you can use in a custom module to do a redirection according to your needs.

First, let’s get the non-coding options out of the way. There are a couple of great modules that you can use to perform redirects. Login Destination I believe is the most representative for this, but there are others as well such as LoginToboggan or Login Redirect. Additionally, you could use the Rules module and other site building techniques found in Drupal 7 core. I have actually covered Login Destination in an older post so feel free to brush up on that if you want.

Nevertheless, you can also do this in your custom module and I will show you how. Perhaps it’s an overkill for you to install one of these modules or your own module needs to define some conditions for the redirect to happen. For this, you can use hook_user_login(). I’m going to show you the code first and then explain:

function mymodulename_user_login(&$edit, $account) {
  if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {
    if(in_array('custom role', $account->roles)) {
      $_GET['destination'] = 'custom/page/of/your/choice';

Looking at this function you’ll notice some odd stuff. Mostly that the whole functionality is wrapped around a .php if conditional that checks for some form POST values.

This makes sure that the redirection does not happen if the user is attempting to reset his password and it is quite important.

Once this is out of the way, another conditional checks if the user logging in has a certain permission role. This is optional and it is where your module can play its part by supplying the condition. You have access to the entire user $account object to create all sorts of conditions (use the Devel module to dpm() the variables and see what’s in them).

Finally, if everything is good, we pass the destination path to the $_GET superglobal’s destination value. And that’s it. Like I said though, you can add all sorts of conditions here that your module comes up with so give it a go. It’s always fun to play around with these things.

And if something is not clear, just drop a line in the comments.

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us


Micha 12 Apr 2013 20:21

Why dont use action and

Why dont use action and trigger? Its available Out of the Box.

pjcdawkins 17 Apr 2013 14:18


if(in_array('custom permission', $account->roles)) {
should be corrected to this:
if(in_array('custom role', $account->roles)) {

The "roles" property contains the role names for the account, not the permissions assigned to that role.

Add new comment