SOLUTION: Below is the solution to this problem. If you would like an automatic redirect to the previous page, here is the code to help you out. A very special thanks to David Peralty for this solution.
Insert this code into your functions.php file
add_filter("gform_confirmation", "custom_confirmation", 10, 4);
function custom_confirmation($confirmation, $form, $lead, $ajax){
// replace with the input number of the hidden field
$redirurl = $_POST["input_2"];
if($form["id"] == "1"){
$confirmation = array();
$confirmation['redirect'] = $redirurl;
}
return $confirmation;
}
add_filter("gform_pre_render_1", "redirecturl");
add_filter("gform_admin_pre_render_1", "redirecturl");
function redirecturl($form){
foreach($form["fields"] as &$field){
if($field["id"] == 2){
$field["defaultValue"] = $_SERVER['HTTP_REFERER'];
}
}
return $form;
}
Three important points:
--Where you see the number 1 is where you will need to fill in the form ID for the form you wish to apply the redirect
--Add a blank text box to your form, the ID of this box is the number you insert where you see the number 2. This text box is where the form will store your redirect url for after submission
--It is important to A) set that field to be dynamically populated (leave it blank though) and to B) hide it using CSS (add a class to form then add a line like .formclass {display: none;} to your css stylesheet)-DO NOT set it to be visible by admin only, it will not work if you do that.
This works great for combining the User Registration add-on and setting forms to "user must be logged in"--place a link to a user registration form in the error message with this enabled and users will be redirected right back to the form after registering (there is also code on this forum for auto-login that really completes the process).
Posted 12 years ago on Friday July 13, 2012 |
Permalink