Generate quiz for Contact Form 7

If you’re using Contact Form 7 for WordPress you might want to protect your forms with a quiz: this is a field where the user has to enter the result of predefined questions. Since we don’t want to annoy the user with a complicated task we’d like to generate very easy to answer questions.

Generating the quiz

We’re going to generate very easy mathematical equations to prevent spam bots from filling out the form. To make things really easy we ask the user to add two numbers where the sum is always smaller than ten. This way it’s a matter of a second to fill out the quiz and the user can complete the form really fast.

Here’s a small shell script that generates the necessary code for the quiz tag.

for i in `seq 1 9`; do
  for j in `seq 1 9`; do
    sum=`expr $i + $j`
    test $sum -gt 9 && continue
    echo -n "\"$i+$j=?|$sum\" "
  done
done
echo

The same script in Perl can be easily executed with perl -e.

for ($i = 1; $i <= 9; $i++) {
  for ($j = 1; $j <= 9; $j++) {
    $sum=$i+$j;
    next unless $sum <= 9;
    print "\"$i+$j=?|$sum\" ";
  }
}
print "\n"'

Use whatever works for you.

Going one step further

Maybe you want to adjust the code above to suit your needs but you’re way too lazy to do this. I thought about that too and setup the following JavaScript. Change the values as you see fit and copy the corresponding code to your form; maybe you want to set the name – quiz-4711 – to something else.

Min number
Max number
Limit

Conclusion

Using a quiz along with Contact Form 7 helped me to eliminate spam submitted through the forms I’ve set up. The quiz is super easy and shouldn’t annoy the user. Since I’ve used the scripts above numerous times I thought about sharing them with everybody who’d like to use the quiz this way.

26 thoughts on “Generate quiz for Contact Form 7”

  1. This is generous of you to share. I just have no idea where to put the script! I took a guess and put it into the Quiz tag between the quotes, but that doesn’t work.

  2. Hi Patty,
    copying the scripts presented above won’t cut it because they were meant to be evaluated manually, i.e. using a shell or Perl interpreter. Instead, just use the generated quiz tag inside the text area under Going one step further; if you want to you can adjust the values min/max number and limit as you see fit.

  3. Pingback: Generate quiz for Contact Form 7

  4. Thanks for taking the time to post that solution. Even if it only saved me 20 minutes of typing, I’ll be sure to use it again. 20 minutes is 20 minutes! 😉

    Just wanted to post my appreciation for your efforts.

    Roger D.

  5. Hello Roger and everybody else,
    thank you for your encouraging feedback, it’s really nice to hear that the little tool helped you. To be honest, I’ve used it so many times now, it has saved me lots and lots of time – it’s great that you’ve saved some time too.

  6. Hmm. I’ve noticed that if you are filling out a contact form but answer the quiz wrong it won’t let you send even if you get it right after that…?

  7. Hi Andrew,
    I’ve just tried that but couldn’t reproduce this issue. If this seems to be a reproducible error on your end please contact the author of CF7 and tell him more about it.

  8. Hi web-profile,
    if you don’t need this parameter simply remove it manually before pasting the code into your form. Other people – including me – need this to style the element with CSS and that’s the reason I added it in the first place.

  9. Hi web-profile,
    I guess you’re better off asking this the developer of CF7 but I think you’ll have to adapt the plugin for this because the markup is generated in its core and normally you don’t have to bother how this exactly works. If you just want to change the styling I recommend using CSS and applying it to the elements of the quiz; this way you don’t necessarily have to change the markup and the plugin which in turn makes updating the plugin easier in the long run.

  10. Thanks for response. Yeah, I know, if I will change the core files and then update the plugin, then everything I’ve done will be destroyed. Ok, I will style it with css. Thanks anyway.
    P.S. I think your reply should be yellow (not gray). Missing class (bypostauthor) for the list item, which wraps your reply.

  11. I have been search for ages for a simple way to include a random maths question in contact form 7. This works perfectly. thank you so much!

  12. Hallo Christian, danke für deine Arbeit, danach habe ich eben gesucht. Eine Frage noch, ich würde gerne vor der Aufgabe noch einen Hinweis eingeben, z.B. Spam-Schutz. Leider schaffe ich das irgendwie nicht, kannst Du helfen?

  13. Hi Benjamin,
    Du kannst in dem Feld, in dem Du den Code für den Quiz einfügst beliebigen Text davor und dahinter schreiben. Bei meinem Kontaktformular sieht das beispielsweise so aus:

    Bitte keine Spam-Bots...
    [quiz ...

    Ersetzen den Satz einfach durch etwas bei dir passendes, speichere das Formular und die Änderung sollte auf der Webseite sichtbar werden.

Comments are closed.