Skip to content

Instantly share code, notes, and snippets.

@rochow
Last active October 12, 2020 22:04
Show Gist options
  • Save rochow/6abbfedd98dcc169484c to your computer and use it in GitHub Desktop.
Save rochow/6abbfedd98dcc169484c to your computer and use it in GitHub Desktop.
Gravity Forms - Redirection Based on Answers
<?php
/*
Usage: You have a questionare. You set the value of each answer in relation to the index+1 in the array below
We then correspond each answer to see which result was selected the most then redirect to that page
For a tie we just redirect to the top item, this could be customised to select one of the top results
*/
add_filter( 'gform_confirmation', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $lead, $ajax ){
if( 1 == $form['id'] ) {
$results = [
[
'count' => 0,
'redirect' => home_url().'/example-1/'
],
[
'count' => 0,
'redirect' => home_url().'/example-2/'
]
];
foreach( $lead as $k => $v ) {
if( is_numeric( $k ) ) {
$results[ $v - 1 ]['count'] = $results[ $v - 1 ]['count'] + 1;
}
}
$tie = false;
$most = 0;
$redirect = 0;
foreach( $results as $k => $r ) {
if ($r['count'] > $most ) {
$most = $r['count'];
$tie = false;
$redirect = $k;
} elseif ($r['count'] ==$most ) {
$tie = true;
}
}
if( false == $tie ) {
$confirmation = [ 'redirect' => $results[$redirect]['redirect'] ];
} else {
$confirmation = [ 'redirect' => $results[0]['redirect'] ];
}
}
return $confirmation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment