<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Christian Schenk&#187; cf7</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/cf7/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generate quiz for Contact Form 7</title>
		<link>http://www.christianschenk.org/blog/generate-quiz-contact-form-7/</link>
		<comments>http://www.christianschenk.org/blog/generate-quiz-contact-form-7/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 16:50:16 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[cf7]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[quiz]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=691</guid>
		<description><![CDATA[If you&#8217;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&#8217;t want to annoy the user with a complicated task we&#8217;d like to generate very easy to answer questions.

Generating the [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using <a href="http://wordpress.org/extend/plugins/contact-form-7/">Contact Form 7</a> for WordPress you might want to protect your forms with a <em>quiz</em>: this is a field where the user has to enter the result of predefined questions. Since we don&#8217;t want to annoy the user with a complicated task we&#8217;d like to generate very easy to answer questions.</p>
<p><span id="more-691"></span></p>
<h2>Generating the quiz</h2>
<p>We&#8217;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&#8217;s a matter of a second to fill out the quiz and the user can complete the form really fast.</p>
<p>Here&#8217;s a small shell script that generates the necessary code for the quiz tag.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">seq</span> <span style="color: #000000;">1</span> <span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #000000; font-weight: bold;">for</span> j <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">seq</span> <span style="color: #000000;">1</span> <span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">sum</span></span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$i</span> + <span style="color: #007800;">$j</span><span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #007800;">$sum</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">9</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">continue</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$i</span>+<span style="color: #007800;">$j</span>=?|<span style="color: #007800;">$sum</span><span style="color: #000099; font-weight: bold;">\&quot;</span> &quot;</span>
  <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">echo</span></pre></div></div>

<p>The same script in Perl can be easily executed with <code>perl -e</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$sum</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$j</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$sum</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>$i+$j=?|$sum<span style="color: #000099; font-weight: bold;">\&quot;</span> &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #ff0000;">'</span></pre></div></div>

<p>Use whatever works for you.</p>
<h2>Going one step further</h2>
<p>Maybe you want to adjust the code above to suit your needs but you&#8217;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 &#8211; <code>quiz-4711</code> &#8211; to something else.</p>
<form action="">
<table>
<tr>
<td style="width:12em;">Min number</td>
<td>
<input type="text" name="min" id="min" size="3" value="1" onkeyup="print_quiz(this.form)"/></td>
</tr>
<tr>
<td style="width:12em;">Max number</td>
<td>
<input type="text" name="max" id="max" size="3" value="9" onkeyup="print_quiz(this.form)"/></td>
</tr>
<tr>
<td style="width:12em;">Limit</td>
<td>
<input type="text" name="limit" id="limit" size="3" value="9" onkeyup="print_quiz(this.form)"/></td>
</tr>
<tr>
<td colspan="2"><textarea id="quiz" cols="60" rows="3">[quiz quiz-4711 class:quiz \"1+1=?|2\" \"1+2=?|3\" \"1+3=?|4\" \"1+4=?|5\" \"1+5=?|6\" \"1+6=?|7\" \"1+7=?|8\" \"1+8=?|9\" \"2+1=?|3\" \"2+2=?|4\" \"2+3=?|5\" \"2+4=?|6\" \"2+5=?|7\" \"2+6=?|8\" \"2+7=?|9\" \"3+1=?|4\" \"3+2=?|5\" \"3+3=?|6\" \"3+4=?|7\" \"3+5=?|8\" \"3+6=?|9\" \"4+1=?|5\" \"4+2=?|6\" \"4+3=?|7\" \"4+4=?|8\" \"4+5=?|9\" \"5+1=?|6\" \"5+2=?|7\" \"5+3=?|8\" \"5+4=?|9\" \"6+1=?|7\" \"6+2=?|8\" \"6+3=?|9\" \"7+1=?|8\" \"7+2=?|9\" \"8+1=?|9\"]</textarea></td>
</tr>
</table>
</form>
<p><script type="text/javascript">
//<![CDATA[
<!--
function print_quiz(form) {
  var min = form.min.value;
  var max = form.max.value;
  var limit = form.limit.value;
  form.quiz.value = "[quiz quiz-4711 class:quiz ";
  for (i = min; i < max; i++) {
    for (j = min; j < max; j++) {
      var sum = i*1 + j*1;
      if (sum > limit) continue;
      form.quiz.value += "\"" + i + "+" + j + "=?|" + sum + "\" ";
    }
  }
  form.quiz.value += "]";
}
//-->
//]]&gt;
</script></p>
<h2>Conclusion</h2>
<p>Using a quiz along with Contact Form 7 helped me to eliminate spam submitted through the forms I&#8217;ve set up. The quiz is super easy and shouldn&#8217;t annoy the user. Since I&#8217;ve used the scripts above numerous times I thought about sharing them with everybody who&#8217;d like to use the quiz this way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/generate-quiz-contact-form-7/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
