August 4, 2021: Added support for randomizing the field order.
February 11, 2021: Added support for saving selected random fields in the entry meta. Also, added support for making all fields random if no field IDs specified.
November 26, 2020: Added support for skipping pages on mulit-page forms where no random fields were visible.
May 18th, 2017: Added support for creating multiple random field groups on a single form. Add form export to support testing with base configuration.
March 17, 2015: Fixed strict standards issues with class properties.
September 17, 2014: Fixed issue where $input string was not properly escaped.
- How do I install this snippet?
- Do I need to modify this snippet to work with my form?
- What else should I know about this snippet?
- Summary
This snippet provides an easy way to show a random number of fields on your Gravity Forms. Let’s say you’re using Gravity Forms to power a simple questionnaire. You have five questions total but only want to show three of those questions per submission. This snippet lets you do that!
How do I install this snippet?
Just copy and paste the code into your theme’s functions.php file.
Do I need to modify this snippet to work with my form?
Yes! This snippet is a little different than some of the other snippets on the site. After you’ve installed the snippet, you can initialize it like this:
new GFRandomFields( 123, 2, array( 1, 2, 4, 5, 7 ), true );
Let me walk you through each of those values and what they represent.
- Update the
123
to the ID of your form. - Update the
2
to the number of random fields you want to show at a time. - Update the
array( 1, 2, 4, 5, 7 )
to the field IDs which should be random. Any field ID you do not include in this array will considered a “static” field and show up on every form load. - Lastly, if you want to randomize the order of the fields, update the
true
tofalse
. By default, the field order set in the form editor is preserved. Setting this tofalse
will randomize the field order on load.
To summarize:
new GFRandomFields( $form_id, $display_count, $random_field_ids, $preserve_order );
What else should I know about this snippet?
Oh, boy. Lots!
- Field validation is skipped for any random field that is not selected for display.
- It works with multi-page forms such that after the first page is submitted, the randomly selected fields will remain the same until the form is submitted (or refreshed).
- Also on multi-page forms, if a page has no fields visible, the page will be skipped.
- It integrates with Save & Continue so if you return later to complete your submission, the randomly selected fields are preserved.
Summary
Make sure you check out the demo to see this snippet in action. Let us know in the comments if you’d like to see this become part of our Gravity Perks suite.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
The two questions in the View Demo link on https://gravitywiz.com/random-fields-with-gravity-forms/ do not get refreshed with new questions when the page is reloaded. I have tried multiple browsers.
The demo link takes me to https://demos.gravitywiz.com/random-fields/
Best Regards
Thanks for pointing this out Dee. The snippet seems to work locally but it doesn’t work on the demo site. I’ll pass this on to our dev team for investigation.
Best,
Hi Dee,
Thanks for the heads-up! It looks like we had too aggressive of caching on that page.
It should be working now.
Hey guys,
Snippet looks to be just what I need, but it is not removing blank pages from my randomised quiz.
The script is working in that I am only seeing 8 questions out of the 15, but the pages with removed questions are still appearing blank.
Is this due to incompatibility with Gravity quiz add-on?
Thanks
Hey Damian, we have a experimental solution for this here: https://github.com/gravitywiz/snippet-library/pull/334
Long term plan is to toughen it up a bit and roll this whole snippet (with the improved support for pages into GP Randomizer).
Hey David,
Thanks for this, although I am having an issue where some times upon refresh the script is calling in a page break ID, I have set “GFRandomFields” as required and 50% of the time is shows just the 8 questions. But sometimes it says 9 questions and one of them is a page break, and I get duplicated “Next page” buttons appear?
Upon close inspection, it is always the final page break ID being called in. Even if I delete page break ID “45”, and create a new one “46” it still gets called in.
Hey Damian, I responded to your comment on the PR as well – given the complicated nature of this configuration, we’d need to ask you to pick up (or upgrade) to an Advanced or Pro license so we can dig in. Hoping that’s doable for you so we can get you sorted!
February 11, 2021: “Also, added support for making all fields random if no field IDs specified.”
Is that the same as shuffling all fields?
For example with X # of fields I’m opening for random order
Form 1
fields = 4,
All fields = 1,2,3,4
new GFRandomFields(1, 4, array(1,2,3,4));
I’ve used this for initialization, but I want 4,2,3,1 or 1,3,4,2 etc I don’t want to exclude fields, but I get everything in order. If i specify 3 fields they stay in order, but the it randomly removes 1.
Above there is a comment about commenting out a line of code. It looks like that code is a little different now, and it doesn’t seem to work to simply comment it out.
suggestion from above:
“
Hi Darin, I think the snippet will work this way if you comment out this line:
add_filter(“gform_form_tag_$form_id”, array(&$this, ‘store_selected_field_ids’));
“
Any suggestion / tips are much appreciated.
Thanks in advance, Joe
Hi Joe,
Currently, the snippet requires the number of fields on the form to be more than the number of fields to display which is why it removes 1. If you have a Gravity Perks License you can get in touch with us via our Support form so we can dig into this further.
Best,