Skip to content

Instantly share code, notes, and snippets.

@timwburch
Last active February 4, 2018 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwburch/9690ef4db4cd5d5a7b2ad8fa3a21b700 to your computer and use it in GitHub Desktop.
Save timwburch/9690ef4db4cd5d5a7b2ad8fa3a21b700 to your computer and use it in GitHub Desktop.
Gravity Forms shuffle fields on current page only
<?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;
}
?>
@jemoreto
Copy link

jemoreto commented Feb 4, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment