Easy anti spam question, without plugin

If you run the standard comments form you'll get so spam every now and then. Quit annoying.. a way to prevent this is captcha. I myself don't like those captchas with weird letters making weird words. I simple question would be much more user friendly and just as effective I think.

There are plugins that do this for you, but I don't like to many plugins in my theme. Many of them generate notices and other errors or conflicts. Some are also over complicated for what I what. So with some searching, copying & pasting and some thinking I've solved it in 33 lines of code.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
<?php add_filter( 'comment_form_defaults',  'add_arspam_to_comment_fields');<br ?-->
function add_arspam_to_comment_fields($default) {
    $commenter = wp_get_current_commenter();
        $default['fields']['url'] .=    '
 ' . //een nieuw veld toevoegen na het url veld
                                        '<label for="arspam">Antispam; Wat is de kleur van de "plaats reactie" knop?</label>
                                        <span class="required">*</span>
                                        <input id="arspam" type="text" maxlength="10" name="arspam" size="30" />
 ';
    return $default;
}
 
if ( isset($_POST['arspam']) ) {    add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
    function verify_comment_meta_data($commentdata) {
        $antwoord = $_POST['arspam'];
        switch($antwoord){
            case "":                wp_die( __('Error: Vul de antispam vraag in aub'), '', 'back_link=true' );
                break;
            case "rood": //de
            case "Rood": //goede
            case "red":  //antwoorden            case "Red":  //zoveel of weinig als je wil
                return $commentdata;
                break;
            default :
                wp_die( __('Error: U heeft het verkeerde antwoord op antispam vraag gegeven'), '', 'back_link=true'  );        }
    }
} ?>

First the functie add_arspam_to_comment_fields  adds a new text field after the field where you fill in your website address ( URL ) . If you use a custom comment_form($args) you can scroll down for a solutions. Let's first continue with the code above.

Now we have the input field we need to say what needs to be done with it. To start we check on line 15 if the input field is indeed there. If you are logged in as a user the form fields will not be displayed and can therefor never be tested. This is not necessary anyway because this means we're dealing with a known user or administrator.

After this we test what has been filled in. When the field has been left empty case "": than wp_die() will fire. wp-die() is the function which ensures that there is nothing further executed except for maybe displaying an error message that may be.

If the answer is correct (line23) then the information entered in the form is further processed via return $commentdata;, any other answer gives another answer given will result in a wp_die because logically this will be the wrong answer.

And that was all , as simple as that ! 🙂
The above code is also available for download for the enthusiast. You will have to edit the source code a bit to your liking.

As promised here is the version if you're using custom fields through comment_form($args). You have to adjust your comments.php to specify the arguments. Here is an example from my own comments.php


1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
65
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
 
$comment_form_args = array(  'id_form'           => 'commentform',
  'id_submit'         => 'Verzend',
  'title_reply'       =>  'Laat een reactie achter',
  'title_reply_to'    =>  'Reageer op %s',
  'cancel_reply_link' =>  'Annuleer de reactie',  'label_submit'      =>  'Plaats reactie',
  'comment_field'     =>  '<p class="comment-form-comment"><label for="comment">
                           <a title="' . sprintf('U kunt deze HTML tags gebruiken: %s','' . allowed_tags() . '') . '">Uw reactie</a></label><br/>
                           <textarea id="comment" name="comment" cols="45" rows="18" aria-required="true"></textarea></p>',
  'comment_notes_before' => '',    'fields'            => apply_filters( 'comment_form_default_fields', array(
 
                        'author' =>
                          '<div id="comment-persoon-detail"><p class="comment-form-author">' .
                          '<a title="' . ( $req ? 'Dit is een verplicht veld' : 'Uw naam' ) . '"><label for="author">Naam ' .                          ( $req ? '<span class="required">*</span>' : '' ) .
                          '</label></a><input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
                          '" size="30"' . $aria_req . ' /></p>',
 
                        'email' =>                          '<p class="comment-form-email"><a title="' . ( $req ? 'Dit is een verplicht veld, ' : '' ) . 'Het e-mailadres wordt niet gepubliceerd"><label for="email">Email ' .
                          ( $req ? '<span class="required">*</span>' : '' ) .
                          '</label></a><input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
                          '" size="30"' . $aria_req . ' /></p>',
                         'url' =>
                          '<p class="comment-form-url"><a title="Het website-adres wordt niet gepubliceerd"><label for="url">Website</label></a>' .
                          '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
                          '" size="30" /></p>',
                              'arspam' =>
                            '<p class="comment-form-arspam">' .
                        '<label for="arspam">'. __('Antispam; Wat is de kleur van de "plaats reactie" knop?') . '
                        <span class="required">*</span></label>
                <input id="arspam" name="arspam" maxlength="10" size="30" type="text" /></p></div>'                        )
                  ),
  'comment_notes_after' => '<div class="clear"></div>',
    
); 
comment_form($comment_form_args); 
 
$comments_list_args = array(
    'walker'            => null,    'max_depth'         => '',
    'style'             => 'ul',
    'callback'          => null,
    'end-callback'      => null,
    'type'              => 'all',    'reply_text'        => 'Reageer',
    'page'              => '',
    'per_page'          => '',
    'avatar_size'       => 0,
    'reverse_top_level' => null,    'reverse_children'  => '',
    'format'            => 'xhtml', //or html5 @since 3.6
    'short_ping'        => false // @since 3.6
); 
wp_list_comments( $comments_list_args );

 

On line 36 you can see I've added 'arspam' to the array and specified the layout for the input field.

I hope this was helpful, let me know!

  • I apologize for my poor English. I should have paid more attention in school...
    If there are any huge mistakes please inform me
  • I've never studied C, Javascript or PHP. Everything I know is learned via youtube and google.
    I realize my code is often unnecessarily long, but I prefer writing it this way so I can still understand myself later on. If there are other mistakes please let me know.