Last active
February 4, 2018 19:12
-
-
Save timwburch/9690ef4db4cd5d5a7b2ad8fa3a21b700 to your computer and use it in GitHub Desktop.
Gravity Forms shuffle fields on current page only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gravity Forms Shuffle fields on current page. | |
* To impliment add this to your functions.php file | |
* adding the form id will isolate the filter e.g. add_filter( 'gform_pre_render_5', 'randomize_field_order'); | |
*/ | |
add_filter( 'gform_pre_render', 'randomize_field_order'); | |
function randomize_field_order ( $form ) { | |
$current_page = GFFormDisplay::get_current_page( $form['id'] ); | |
$fields = $form[fields]; | |
function filter_by_key($array, $member, $value, $member2, $value2) { | |
$filtered = array(); | |
foreach($array as $k => $v) { | |
if($v->$member == $value && $v->$member2 != $value2) | |
array_push($filtered, $k); | |
} | |
return $filtered; | |
} | |
$pageFields = filter_by_key($form['fields'],'pageNumber',$current_page,'type','page'); | |
function swap(&$a, &$b) { list($a, $b) = array($b, $a); } | |
for($i = 0; $i < count($pageFields); $i++) { | |
$j = rand(1, count($pageFields)) - 1; | |
swap($fields[$pageFields[$i]], $fields[$pageFields[$j]]); | |
} | |
$form[fields] = $fields; | |
return $form; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this.
Just to let you know, I had to put quotation marks between "fields", on lines 15 and 34:
$form['fields'] = $fields;
Cheers