knackforge
December 9, 2011
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.
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.
Just like how your fellow techies do.
We'd love to talk about how we can work together
Take control of your AWS cloud costs that enables you to grow!