How To Redirect User To Any Page After Login in Drupal 7

There are loads of modules that can do this for you, but why to install one more module if you can do this with one string of code? The best thing is that approach below does a redirection even if you login from user login block.

Just put the following code into your custom module:

 * Implements hook_user_login().
function module_name_user_login(&$edit, $account) {
  // Don't redirect on password reset.
  $current_menu_item = menu_get_item();
  if ($current_menu_item['path'] == 'user/reset/%/%/%') {
  // Redirect user to profile page after the login.
  $_GET['destination'] = 'user';

You can find this snippet at here:
UPDATE 03/08/2013: Added several lines to prevent redirection during password reset.

Now to a little explanation what we've done here: we used user_login() hook that fires when user logs in. We just set $_GET['destination'] value and profit!

The best part is that this approach works when user log in from the login block. No Rules, no trigger, no any other module I've tried didn't do this for me.

Happy Drupal coding and don't forget to check Drupal snippets repository time from time, there you can find real gems!


Submitted by loopduplicate on Mon, 2013-07-29 18:42

I wouldn't just set the $GET value because what if you ever want to make a link where the destination is something else? ie. /user/login?destination="special-promo-page". Also, I'm not sure this will work when you use a one-time login link. Anyway, I would really recommend just making a rule with the Rules module. There is also this comment on the docs page for hook_user_login:!user!user.api.php/function/hoo...

Whatever you do, document it so that the next person who has to work on the site (could be you in a year) will know what is going on.

Submitted by Tim on Mon, 2013-07-29 19:09

The trick is that Rules won't always work, additionally, installing Rules just to get redirect to work is not my favorite approach.

Setting $_GET is just fine, you can always check if something is in $_GET['destination'] and apply it if needed.

Believe me, I do always document my code ;)

Submitted by Patrick Koroma on Thu, 2013-08-01 18:19

Will this affect the reset password page ?

Submitted by Mario Hernandez on Fri, 2013-08-02 00:56

Thanks for a great article. I'm always open to other ways of doing things. I personally would use rules especially because I would have more control as an admin rather than going into the code to change the destination. Rules makes it easier for non-technical people to manage the behavior as well.

Still great article though.

Submitted by Tim on Fri, 2013-08-02 15:55

as I told, Rules won't redirect user if user logged in via standard user login block try yourself.

Submitted by Greg on Fri, 2013-08-02 19:16

I'd love to use this... Except we need password reset to not be broken by the redirect. Anyone managed to successful exclude password resets from this redirect?

We've used login destination module, but we've had conflicts between it and entityforms.

Submitted by Tim on Sat, 2013-08-03 16:12

Yes, this is easy, I update the snippet, now it should work with password reset.

Add new comment

You are here