Hacking The Challenge Question

In my war against spammers I installed a Challenge Question Plugins that ask you for the year before it will post your comments. This Plugin has helped to reduce the number of spams this blog gets. However, there was one major problem with the Plugin.

If you forget to answer the question before submitting the comment, the Plugin takes a new page asking you to answer the question. The problem was, when you go back to the comment page, your comment was gone. This would be very frustrating if you spent a long time writing a detail comment.

To fix that problem, I had coding guru Justin code up a fix. Instead of taking you to a new page if you forget to answer the challenge question, you now get this pop up message.

question1.jpg

If you somehow get the answer wrong (for whatever reason), you will get this pop up message.

question2.jpg

If you try submitting a blank comment, you will get a pop up message as well but I won’t show you what it says. You can try it yourself. I posted about this fix two days ago and many readers have asked how they can put this hack on their blog. Here is how you do it.

Step 1 – Codes For the Head

Place the follow code inside your blog’s <head> tags. It can go anywhere within the tag just as long as it’s between <head> and </head>.

<script type="text/javascript">
function get_answer(field,alerttxt)
{
with (field)
{
if (value!="2007")
{alert(alerttxt);return false}
else {return true}
}
}
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{alert(alerttxt);return false}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(author,"Name must be filled out!")==false)
{author.focus();return false}

if (validate_required(email,"Email must be filled out!")==false)
{email.focus();return false}

if (validate_required(answer,"Please answer the question! It’s 2007 in case you
didn’t know.")==false)
{answer.focus();return false}

if (get_answer(answer,"You don’t know what year it is? Here’s a clue. The answer
is 2007!")==false)
{answer.focus();return false}

if (validate_required(comment,"Umm… you going to say anything?")==false)
{comment.focus();return false}
}
}
</script>

There is no need to change any of the codes that came with the Challenge Question Plugin. The above code just overrides the way it displays the wrong, or no answer. You can customize the codes to give you own custom responses if you don’t like mine.

If you are also running Brian’s Threaded Comments Plugin, then you will need to make one more code change before the Justin hack will work.

Step 2 – Comment Out & Add

Open up your comments.php file in your current WordPress theme and look for
the following line:

<input onclick="if(typeof(onAddComment) == ‘function’) { onAddComment(); }
else { alert(‘ERROR:
It looks like the website administrator hasn\’t activated
the Brians Threaded Comments plugin from the plugin page’); };" name="addcommentbutton"
type="button" id="addcommentbutton" value="Add comment" />

Comment out the above code (or delete it) and directly below it add the
follow line of code:

<input type="submit" name="Submit" value="Submit">

Once again, step 2 is only for people running Brian’s Threaded Comments. If
you haven’t done anything to your comments.php file, then you only need to do
step one.