This snippet is deprecated.
Gravity Forms v1.7 provides some really awesome support for conditional confirmations right out of the box. Consider this snippet deprecated.
This snippet provides the ability to specify conditional confirmations in Gravity Forms. Want to redirect users to a different page based on a value they select on the form? Or just show them a different confirmation message? This functionality will be available in Gravity Forms relatively soon, but for those of you who need this now, here is a nice an easy solution!
<?php | |
/** | |
* Conditional Confirmations (with Merge Tag Support) | |
* http://gravitywiz.com/2012/08/18/conditional-confirmations/ | |
* | |
* Provides the ability to register conditional confirmations. To register a new conditional confirmation | |
* use the GWConditionalConfirmations::add_conditional_confirmation() function. | |
* | |
* GWConditionalConfirmations::add_conditional_confirmation($form_id, $field_id, $operator, $value, $confirmation); | |
* | |
* @param mixed $form_id The ID of the form for which you would like to register a conditional confirmation. | |
* @param mixed $field_id The field ID of the field for which you would like to base the confirmation condition. | |
* @param mixed $operator The operator which will be used to compare the submitted field value against the specified value | |
* passed in the $value parameter. Accepted values are "is", "isnot", "greater_than", "less_than", "contains", "starts_with", "ends_with" | |
* @param mixed $value | |
* @param mixed $confirmation Accepted values are: | |
* array('redirect' => 'http:://yoururl.com') | |
* array('page' => 12) | |
* 'Plain text confirmation!' | |
* @param $do_init Defaults to true. Will automatically run the init which will trigger the conditional functionality when the | |
* form is submitted. If you would like to init on your own, you can pass false here and call the GWConditionalConfirmation::init() | |
* function anytime before the gform_confirmation hook is called. | |
*/ | |
class GWConditionalConfirmations { | |
public static $init = false; | |
public static $confirmations = array(); | |
public static function init() { | |
add_filter('gform_confirmation', array( __class__, 'get_conditional_confirmation'), 10, 3); | |
} | |
public static function add_conditional_confirmation($form_id, $field_id, $operator, $value, $confirmation, $do_init = true) { | |
if(!self::$init && $do_init) { | |
GWConditionalConfirmations::init(); | |
self::$init = true; | |
} | |
if(!isset(self::$confirmations[$form_id])) | |
self::$confirmations[$form_id] = array(); | |
$confirmation['gwConditionalLogic'] = array('field_id' => $field_id, 'value' => $value, 'operator' => $operator); | |
array_push(self::$confirmations[$form_id], $confirmation); | |
} | |
public static function get_conditional_confirmation($confirmation, $form, $lead) { | |
if(!isset(self::$confirmations[$form['id']])) | |
return $confirmation; | |
foreach(self::$confirmations[$form['id']] as $conf) { | |
if(self::is_condition_met($form, $conf['gwConditionalLogic'])) | |
return self::convert_confirmation($conf, $form, $lead); | |
} | |
return $confirmation; | |
} | |
public static function is_condition_met($form, $condition) { | |
$field = RGFormsModel::get_field($form, $condition['field_id']); | |
$is_visible = !RGFormsModel::is_field_hidden($form, $field, array()); | |
$field_value = apply_filters('gw_condition_field_value', RGFormsModel::get_field_value($field, array()) ); | |
$is_value_match = RGFormsModel::is_value_match($field_value, $condition['value'], $condition['operator']); | |
return $is_value_match && $is_visible; | |
} | |
public static function convert_confirmation($confirmation, $form, $lead) { | |
// if redirect is set, return as is | |
if(isset($confirmation['redirect'])) { | |
$confirmation['redirect'] = GFCommon::replace_variables( trim( $confirmation['redirect'] ), $form, $lead, true ); | |
return $confirmation; | |
// if page is set, return as redirect with permalink | |
} else if(isset($confirmation['page'])) { | |
return array('redirect' => get_permalink($confirmation['page'])); | |
// if nothing else, assume it is text and wrap in the confirmation HTML | |
} else { | |
return "<div id='preview_form_container'>" . GFCommon::replace_variables($confirmation, $form, $lead, true) . "</div>"; | |
} | |
} | |
} | |
// example for form ID 7 where the confirmation will redirect the user to http://google.com if the value of field ID 3 is less than 10 | |
GWConditionalConfirmations::add_conditional_confirmation(7, 3, 'less_than', 10, array('redirect' => 'http://google.com')); | |
// example for form ID 5 where a text confirmation will be displayed if field ID 2 is equal to "Virginia" | |
GWConditionalConfirmations::add_conditional_confirmation(5, 2, 'is', 'Virginia', 'Confirmed! You are from Virginia!'); | |
// example for form ID 11 where the confirmation will redirect to the WordPress page ID 12 if the value of field ID 4 is greater than 500 | |
GWConditionalConfirmations::add_conditional_confirmation(11, 4, 'greater_than', 500, array('page' => 12)); |
How do I install this snippet?
Just copy and paste this snippet to your theme’s functions.php file.
Do I need to configure this snippet to work with my form?
Yes. First of all, you’ll want to remove the examples from the bottom of the snippet that you do not need. Next, you’ll want to either modify one of the examples to suit your needs or you can create your own.
GWConditionalConfirmations::add_conditional_confirmation($form_id, $field_id, $operator, $value, $confirmation);
- The
$form_id
parameter should be the ID of the form for which you would like to register a conditional confirmation. - The
$field_id
parameter should be the ID of the field for which you would like to base the confirmation condition. - The
$operator
parameter should be the operator which will be used to compare the submitted field value against the specified value passed in the $value parameter. Accepted values are"is"
,"isnot"
,"greater_than"
,"less_than"
,"contains"
,"starts_with"
, “ends_with"
. - The
$value
parameter is the value to which submitted field value will be compared to. - The
$confirmation
parameter should be the type and corresponding value of that confirmation type. Acceptable values are:array('redirect' => 'http:://yoururl.com')
array('page' => 12)
'Plain text confirmation!'
Make sure you take another look at the examples too:
<?php | |
// example for form ID 7 where the confirmation will redirect the user to http://google.com if the value of field ID 3 is less than 10 | |
GWConditionalConfirmations::add_conditional_confirmation(7, 3, 'less_than', 10, array('redirect' => 'http://google.com')); | |
// example for form ID 5 where a text confirmation will be displayed if field ID 2 is equal to "Virginia" | |
GWConditionalConfirmations::add_conditional_confirmation(5, 2, 'is', 'Virginia', 'Confirmed! You are from Virginia!'); | |
// example for form ID 11 where the confirmation will redirect to the WordPress page ID 12 if the value of field ID 4 is greater than 500 | |
GWConditionalConfirmations::add_conditional_confirmation(11, 4, 'greater_than', 500, array('page' => 12)); |
Summary
If you come across any bugs, let me know in the comments. Hope this proves to be a good stop gap solution until this functionality makes it into the core. :)
Hi David, excellent work here man! I am in a similar situation as Joseph from above. I am needing the condition to be a number range rather than just 1 comparison. Have you made any progress on this addition to the snippet? This would help me so much right now.
Hi Aaron, this snippet is deprecated. Gravity Forms 1.7 provides support for conditional confirmation right out of the box. :)
One more question. I’m using a dropdown menu with values. What would the format of the merge tag be if I want the value of the merge tag? {Name :3:value} ?
Yep: {Field Label:3:value}
Awesome! Thankyou! Thankyou! Thankyou!
Could you post an example with a merge tag ?
Hi Twannerman, sure thing:
GWConditionalConfirmations::add_conditional_confirmation(7, 3, 'less_than', 10, array('redirect' => 'http://yoururl.com/?first_name={Name (First):3}'));
If you want to make sure you get the merge tag right, just go to your form confirmation and under the “Redirect” tab, enable the “Pass Field Data Via Query String”. This will reveal a drop down of merge tags available for your query string.
Hi, excuse my ignorance but where to I put the GWConditionalConfirmations::add_conditional_confirmation(7, 3, ‘less_than’, 10, array(‘redirect’ => ‘http://google.com’));?
in the functions.php file or in the form somewhere?
thanks
Hi Craig, this goes in your functions.php file.
Hi David,
Are merge tags supported for query strings in redirect? I’m having trouble getting it to work.
Hi Ryan, it does not support this by default. Give this customized version a shot (linked in the comment):
https://gravitywiz.com/2012/08/18/conditional-confirmations/#comment-1352
Worked like a charm David.
The “with Merge Tag Support” in your snippet and the comment above made me think the snippet in your post had been updated. Thanks for the clarification. I can confirm that the modified version worked for me with merge tags. Thanks!
Hi Ryan, you’re correct and there was some sort of fail on my end. The snippet is supposed to have support for merge tags by default. I’ve updated the primary snippet with the merge tag support version. Thanks for pointing that out. :)
You sir are a genius and a lifesaver! Thank you for making this so simple for us non developer folks
Glad you’re finding it useful! :)
Hey David, when is 1.7 coming out?
That is a question for RocketGenius. :)
Hi David…
could this be done for the animation we worked on?
// example for form ID 5 where a text confirmation will be displayed if field ID 2 is equal to “Virginia” GWConditionalConfirmations::add_conditional_confirmation(5, 2, ‘is’, ‘Virginia’, ‘Confirmed! You are from Virginia!’);
Hi Joseph,
This won’t really work for the chart animation you have set up as this was configured as a page template due to all the special scripts/files required for that functionality. If you’d like to customize this, send me an email with some details and I can advise.
Can I send one user to Paypal and a thank you page afterwards and show another user a text message instead of sending them to Paypal with the conditional confirmations?
David, a huge thanks for posting this solution. I was able to grab this snippet and use it for a client this past week. They’ve got a Gravity Forms form that doubles as a user registration form and an application to view private documents. Depending on the user’s input, the form redirects them to a page 1 (access denied) or a page 2 (access granted).
Awesome! Glad to hear you were able to use this out in the wild.
Couldn’t conditonal shortcode be the answer?
http://www.gravityhelp.com/documentation/page/Shortcodes
You can nest shortcodes:
[condition1][condition2]Your text[/condition2][/condition1]
The conditional shortcode would work well if you’d just like to display different versions of a text confirmation. However, if you want one condition to redirect the user to a URL, one condition to show them a text confirmation, or multiple different redirect URLs, the shortcode wouldn’t allow you to accomplish that.
I have been trying to get this to work but no matter what I try instead of redirecting on the conditional statement it instead loads the second page (header included) below the form? Any ideas.
This snippet does not work with ajax-enabled forms. I’m not eager to invest too much effort into this script as conditional confirmations will be available natively in Gravity Forms 1.7; however, if enough people need this now I can see about adding support for ajax-enabled form.
would I still be able to use query strings?
I’m guessing no right?
This modification would allow you to. I just wrote this on the fly and have not been able to test it. Let me know how it works and I’ll update the original snippet above:
https://raw.github.com/gist/3708519/e4fbb5186012ac9bab8802492e18f68ab5a5fc97/mergetagssupport.php
How would I add more than one conditional logic
like
Less than 40
&
Greater than 30
Am i being a Dumb Ass
Hi Joseph, This snippet does not support multiple conditions. It wouldn’t be much work to add support. If this is something that would provide a lot of benefit to you, I can add it to the queue for this snippet. :)
You are such a http://www.youtube.com/watch?v=U6UCHk5pFmU
Will try this
Haha, nice video. And thanks!
Looks like David finally fixed the “reply” issue.