Quantcast
Channel: Snippets Archives - Gravity Wiz
Viewing all articles
Browse latest Browse all 196

Conditional Confirmations

$
0
0

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!

This snippet does not support ajax-enabled forms. Read this comment for more details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?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:

1 2 3 4 5 6 7 8 9 10
<?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));
view raw gistfile1.php This Gist brought to you by GitHub.

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. :)


Viewing all articles
Browse latest Browse all 196

Latest Images

Trending Articles



Latest Images