• 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.

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.


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?