According to documentation it should be possible to redirect any form to a custom page of choice using $form['#redirect'].

So I've got someting like this: (in my template.php)

function phptemplate_user_register($form) {
$form['#redirect'] = 'node/124';
$output = drupal_render($form);
return $output;
}

But it just doesn't work. After registration the user is just redirected to the frontpage. I can't find much information on the web about this and according to the drupal pro development book, it should be working...

Comments

helpermedia’s picture

I think the redirect to the frontpage finds place somewhere in the user module during the registration process and overrides your redirect.

Is the new user logged in after registration? If so, maybe the module Login Destination ( http://drupal.org/project/login_destination ) offers the functionality you want?

Another solution would be creating a module in which you override hook_user and make use of function drupal_goto.

askibinski’s picture

No the user is not logged in afterwards, it's registration only.

It seems #redirect has no effect in other forms too - like the personal contact form. There just isn't enough documentation about this - I can't seem to find any working examples...

Albert Skibinski - Homepage

helpermedia’s picture

Does the url have a destination?

Don't know if it's possible to change the url in theming, but adding "destination=node/124" as url parameter should do the trick also.

askibinski’s picture

I don't want to use destination. According to the drupal pro development book (1st edition - page 153, 2nd edition - page 228) it should be possible to do it this way...

Albert Skibinski - Homepage

helpermedia’s picture

I think it works OK if you're creating custom forms on your own, but in this case the registration process overrides the form redirect.

btw, I saw this thread, maybe it helps -> http://drupal.org/node/202421

askibinski’s picture

Not very consistent behaviour however. You can override most other aspects using $form.

Albert Skibinski - Homepage

askibinski’s picture

Ok, I got it. It works just fine with #redirect as described above, BUT only when you do implement this using hook_form_alter() in your own module. It doesn't work in template.php (though other overrides work well that way).

Albert Skibinski - Homepage

helpermedia’s picture

Nothing beats own modules :-)

Anonymous’s picture

I can redirect away from the user-register form by altering the form (mymodule_user_register_alter()) and either setting the desitination or the #redirect property. That works fine.

However, I need to work with the newly created user object before redirecting, so my redirect has to be located in my submit handler. Setting $form_state['redirect'] = 'url'; is supposed to work. But on this form it doesn't.

I've verified that my submit handler is running AFTER the user module's submit handler.

Anyone have any luck redirecting away from the user-register form from within a submit function?

ThePrince’s picture

What's the exact code then?

I've got:

<?php

function hook_form_alter($form_id, &$form) {
	if($form_id == 'user_register') {
		$form['#redirect'] = 'registration-page';
		$output = drupal_render($form);
		return $output;
	}
}

?>

In a module and it does not work, what am I missing?

Thanks

askibinski’s picture

Replace 'hook' in the function name with your module name.

Albert Skibinski - Homepage

joshk’s picture

form_alter (like all the alters), doesn't want to do any output. You don't need to render/return your form right there. Just make the change, and let drupal's normal processing handle the rest.

------
Personal: Outlandish Josh
Professional: Chapter Three

------
Personal: Outlandish Josh
Professional: Pantheon

chapi’s picture

This code works for me in Drupal 6

<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
	
	if($form_id=="user_register")
	{
		
	
		$form['destination'] = array('#type' => 'hidden', '#value' => "URL_TO_REDIRECT");
		
	}
	
	
}

?>
jduhls’s picture

nice! works like a charm! ergo - "destination" is sought after in $_POST and not just in $_GET ($_REQUEST, I assume). Good to know!

rickshaw5k’s picture

Hi, I'm attempting to implement this on a new drupal site I've been working on and haven't been able to get the redirect working right. Here's what I've got so far, I just made a simple module for the specific purpose of redirecting users to a page once they've registered for the site:

registration_redirect.info:
name = Registration Redirect
description = A module for redirecting users on registration success
core = 6.x

registration_redirect.module:

<?php

function registrationredirect_form_alter(&$form, &$form_state, $form_id) {
    
    if($form_id=="user_register")
    {
        $form['destination'] = array('#type' => 'hidden', '#value' => "http://www.google.com");        
    }
}

I'm hoping there has been a simple mistake on my page, any help or a nudge in the right direction is much appreciated! Also, the actual destination page will be some page I'll make at a later date, just want to get the functionality there before moving further.

devsanjeev’s picture

I think your module name is registration_redirect but you used registrationredirect for hook_form_alter. So make this change, I hope it works.


function registration_redirect_form_alter(&$form, &$form_state, $form_id) {
   
    if($form_id=="user_register")
    {
        $form['destination'] = array('#type' => 'hidden', '#value' => "http://www.google.com");       
    }
}
jesse.ivy’s picture

I used the Login Destination module (http://drupal.org/project/login_destination) which allows you to specify a redirect url based on the path of the login in the administration configuration. I specified /user/register and it worked like a charm. http://www.jesox.com/posts/login-destination-module-preview