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.

4 comments ↓

  • Patty Ayers says:
    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.
  • 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.
  • Patty Ayers says:
    Hi Christian – Ok, that makes sense! I can just use the generated tag. Much appreciated!
  • Brad says:
    Oh, this is great. Many thanks.

Leave a Comment