[et_pb_section admin_label=”section”][et_pb_row admin_label=”Row”][et_pb_column type=”2_3″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

  • What vegetable would you be if you were re-incarnated as a plant?
  • Which Game of Thrones character are you most like?
  • What is your preferred communication style?

Whether useless, fictionally interesting or socially valuable, all of these types of quizzes rely on a common functionality: Ask a series of questions and tally up their answers against some criteria to determine what tops their list. I was happily surprised to see how little code I had to write with Gravity Forms to build a solution that could be re-used for any number of quizzes.

This is a nice simple snippet to share as initial example of Gravity Forms In Action.

[/et_pb_text][/et_pb_column][et_pb_column type=”1_3″][et_pb_image admin_label=”Image” src=”https://idealienstudios.com/wp-content/uploads/Gravity_In_Action.jpg” show_in_lightbox=”off” url_new_window=”off” animation=”left” sticky=”off” align=”left” force_fullwidth=”off” always_center_on_mobile=”on” use_border_color=”off” border_color=”#ffffff” border_style=”solid”] [/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_image admin_label=”Image” src=”https://idealienstudios.com/wp-content/uploads/communication_style.jpg” show_in_lightbox=”off” url_new_window=”off” animation=”left” sticky=”off” align=”left” force_fullwidth=”off” always_center_on_mobile=”on” use_border_color=”off” border_color=”#ffffff” border_style=”solid”] [/et_pb_image][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

Terrific Tallies with one magic merge tag

  1. Create your questions as radio buttons
  2. Use the Show Values to specify the common titles to track
  3. Add {tally_results} to your confirmation or notification
  4. Save your form
  5. Paste the code from this gist into your functions.php or appropriate theme / plugin file location.

[/et_pb_text][et_pb_text admin_label=”Gist” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

https://gist.github.com/Idealien/ca3a3e86ede360c3e2c1

 

[/et_pb_text][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” text_font_size=”14″ use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

What’s it doing?

  1. Loop through every field to check whether it is a radio button field type.
    You could change this to use a field CSS class if you were using radio buttons for other things on the same form.
  2. Inspecting the array – Score – that uses the field value as key.
  3. Creating a new entry or incrementing the value by 1 for each result
  4. Looping through the results and output into the merge tag $result.
    You could change this to sort by most results or filter to only display the top result.

A very simple set of 20 lines of code that mean content authors can create any number of quizzes and not have to deal with complex arrays or other code snippets.

What other merge tags have you made to make?

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Similar Posts