blog-banner

How to Redirect An User After Logging into a Drupal 7 Site?

  • Drupal
  • Drupal Planet
  • from Api

Drupal Global Redirect

 

The main intention of this post is to provide a basic idea about coding which will redirect the users to a custom page once after logging in. The idea of the post was successfully tested in a sample Drupal 7 site and worked very well in all aspects. Once a user logs in, he will be redirected to his profile page. Is that what he needs to see? Apparently not, in that case, it is essential for the site admins to redirect the user to the page that he needs. Note here, at this point we are not speaking about any dynamic customization for each and every user, it is all about redirection to a different structural page that is common to every user. Like a home page on Facebook or a mailbox in Gmail. Structural here means the outlook and not the identical contents.

Where and how to redirect the user after logging in?

The user can be redirected to the home page instead of his profile page. This redirection can be done with the help of a custom code. Code, not more than 10-15 lines. This in fact can attract many users to be a part of your site.

How and where to write these codes?

The most expected question of all times. As this redirection mainly deals with your registration form, a Form API can handle this function in a fair manner. Yes of course it may need a custom module to do this. The custom module will implement a hook_form_alter() API. The arguments for this form API are $form_id, $form and the $form_state. The $form_id implies the name of the form, the $form indicates the structure of the form and the $form_state holds the values of the form. It is necessary to pass these arguments to a form submit handler to make this redirection happen. The following code explains the whole process of redirection.

  1. /**
  2.  * Implements hook_form_alter()
  3.  */
  4. function example_form_alter(&$form, &$form_state, $form_id) {
  5.   if ($form_id == 'user_login') {
  6.     array_unshift($form['name']['#element_validate'], 'example_user_login_form_validate');
  7.     $form['#submit'][] = 'example_user_login_form_submit';
  8.   }
  9. }
  10.  
  11. /**
  12.  * Custom submit handler
  13.  */
  14. function example_user_login_form_submit($form, &$form_state){
  15.     //Redirects to home page on login.
  16.   $form_state['redirect'] = '<front>';
  17. }

The function name to implement the form can be of your own wish. Once a custom function has been created the next step is to set the redirection path.

How to set the redirection path?

This can be done in not more than one line. The following snippet can be placed inside the function for an effective redirection.

$form_state['redirect'] = '<front>';

The '<front>' specifies the register form to redirect itself to the home page (a.k.a. front page) after it has been filled.

This single line can affect the usage of the site a great deal. This redirection can be done to any custom page like gallery, comments, or of any wish.

Get awesome tech content in your inbox