1

I have been trying to redirect users after filling out the user registration if referred from a specific page on the site with form_alter like so

function custom_markup_form_alter(&$form, &$form_state, $form_id) {

if($form_id == 'user_register_form' && strpos($_SERVER['HTTP_REFERER'], 
'/resources/price-sheets') > 0 ) {

    $form_state['redirect'] = 'node/50';

    }
}

If I dpm($form_state) on the user/registration page it shows the $form_state['redirect'] = 'node/50'; as it should. But when I submit the form it redirects me to the home page. I also tried replacing $form_state['redirect'] = 'node/50'; with $_GET['destination'] = 'node/50'; with the same results. I've found a few similar questions, but none of those answers are working for me either, does anyone have a good working solution?

  • are you using any login-related module like logintoboggan or email registration? these modules often have built-in redirects that can mess with this. – longboardnode Oct 16 '17 at 15:57
  • Good thought, but I don't have either of those, and I reviewed the rest of the installed modules but did not see any login/redirect related. – phandolin Oct 16 '17 at 17:21
2

The user register form isn't a normal form in the sense that its validation and submit are overridden by a number of functions in core's user.module including creating the account, sending out the confirm email, and then logging the user in. So I don't think a simple form_alter for the form redirect will help you because it will be overridden later by the login function.

Also you can't simply replace $form_state['redirect'] = 'node/50'; with $_GET['destination'] = 'node/50'; The destination querystring needs to be part of the page's url BEFORE the register form is served. So adding it as part of the form_alter I don't think will change anything. What you can do is:

if($form_id == 'user_register_form' && strpos($_SERVER['HTTP_REFERER'], 
'/resources/price-sheets') > 0 && !isset($_GET['destination'])) {
    drupal_goto('user/register?destination=node/50';
    }
}

Alternately, you can also try to use the hook_user_login, detailed in user.api.php and add your redirect there which will fire after the user has registered successfully (and is now logged in).

One other solution would be to use the Login Destination module which:

The Login Destination module allows you to customize the destination that a user is redirected to after logging in, registering to the site (7.x), using a one-time login link or logging out (7.x). The destination can be an internal page or an external URL. It is possible to specify certain conditions like referring pages or user roles and make the destination depend upon them.

| improve this answer | |
  • The drupal_goto('user/register?destination=node/50'); worked exactly as I wanted, thanks! It was apparent there was much more at play with the user registration process. – phandolin Oct 18 '17 at 13:08
2

I think you need to set the redirect in a submit handler. Add your custom submit handler to $form and then in your custom submit handler set $form_state['redirect'] to where you want it to go. Doing it in a form_alter could be why it's not working. See Redirecting a Form

function my_redirect_form_my_content_type_node_form_alter(&$form, &$form_state){
  $form['#submit'][] = 'my_redirect_node_form_submit';
}

function my_redirect_node_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'node/10'; // the address of your thank you page
  // if you are really, really determined to force the redirect, even if a destination has been set in the url, add this:  unset($_GET['destination']);
  // you could also add a message (this is in addition to whatever message is produced by the nodeapi function): drupal_set_message(t('Congratulations!'));
}
| improve this answer | |
  • Thank you! I have set the following code, and I'm seeing the first part working in dpm($form)['#submit'] = 'custom_markup_user_register_submit' But the second function does not look like it's working as I see no dpm($form_state); nor redirect. function custom_markup_form_alter(&$form, &$form_state, $form_id) { $form['#submit'][] = 'custom_markup_user_register_submit'; dpm($form); } } function custom_markup_user_register_submit($form, &$form_state) { unset($_GET['destination']); $form_state['redirect'] = 'node/50'; // the address of your thank you page dpm($form_state); } – phandolin Oct 16 '17 at 16:42
  • @phandolin Make sure you clear all caches – Beebee Oct 17 '17 at 9:59
  • Thank you, this method worked on a regular form when I tested it, but the user registration form adds a bit of complication. See @longboardnode 's answer for what worked for me. – phandolin Oct 18 '17 at 13:12
  • Didn't notice that you were trying to alter the registration form. Glad you got it sorted. – Beebee Oct 19 '17 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.