Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughHow to redirect a user after login to a specific page
There are some simple Drupal modules that help with login redirection (especially Login Destination), but I often need more advanced conditions applied to redirects, so I like being able to do the redirect inside a custom module. You can also do something similar with Rules, but if the site you're working on doesn't have Rules enabled, all you need to do is:
- Implement
hook_user_login()
. - Override
$_GET['destination']
.
The following example shows how to redirect a user logging in from the 'example' page to the home page (Drupal uses <front>
to signify the home page):
<?php
/**
* Implements hook_user_login().
*/
function mymodule_user_login(&$edit, $account) {
$current_path = drupal_get_path_alias($_GET['q']); // If the user is logging in from the 'example' page, redirect to front.
if ($current_path == 'example') {
$_GET['destination'] = '<front>';
}
}
?>
Editing $edit['redirect']
or using drupal_goto()
inside hook_user_login() doesn't seem to do anything, and setting a Location header using PHP is not best practice. Drupal uses the destination parameter to do custom redirects, so setting it anywhere during the login process will work correctly with Drupal's built in redirection mechanisms.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web