Implementing hook_user_insert doesn't login my user
I'm trying to forward the users to a specific page after they create their account, but when they get there, they are not logged in.
functi开发者_Go百科on mymodule_user_insert(&$edit, $account, $category) {
drupal_goto("node/3");
}
Should I add something to make sure the user gets logged in, after his account is created?
hook_user_insert() is invoked after a user account is being inserted in the database table, which would happen when one of administrator users creates a user account from the administrative pages, or when a module programmatically create a user account.
The purpose of the hook is to save custom data that the module associates with the user account. Calling drupal_goto()
in that case is wrong.
To redirect a user after registration, you could implement code similar to the following one. ("mymodule" is just an example of short name for a module; change it to the short name of the module you are using/creating.)
function mymodule_form_ user_register_form_alter(&$form, &$form_state) {
// Add a form submission handler to the registration form.
$form['#submit'][] = 'mymodule_user_register_submit';
}
function mymodule_user_register_submit($form, &$form_state) {
$form_state['redirect'] = 'where you want to redirect the user';
}
Consider that when this code is executed, the users still need to log in, and (depending on the site settings) they could need to first receive the email that Drupal sends to the users, when it is set to first verify the email address reported by the registering users.
If you want to redirect the user after the login, you should implement hook_user_login(), but the user needs to first log in.
That's not the place to do it, as that should only be used to post custom additions to the database, see the documentation here.
The easiest way to do it would be to use the login destination module as that is already tried and tested and does what you are after. To see how they do it take a peek at the source.
http://drupal.org/project/login_destination
Alternatively you should add your code to hook_user_login
精彩评论