How do I make wordpress comment fields required?

Here is my form:

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform" class="validate">  
   <?php if($user_ID) : ?>  
   <?php else : ?>
   <div class="label_container">
      <div class="left">
         <label for="author" class="label label_name">Name*</label>                 
      </div>
      <div class="right">
         <label for="email" class="label label_email">Email*</label>              
      </div>
      </div>
   <div class="input_container">
      <input id="name" name="author" class="required input_text input_name" type="text" value="" /> 
      <input id="email" name="email" class="required input_text email input_email" type="text" value="" />
   </div>         
   <?php endif; ?>
      <div class="label_container"> 
         <label for="comment" class="label label_comment">Comment</label>                 
      </div>
   <textarea id="comment_box" class="required input_comment" name="comment" cols="40" rows="6"></textarea>   
   <p><input name="submit" class="input_submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />  
   <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /></p>  
   <input type="hidden" id="redirect_to" name="redirect_to" value="<?php echo get_bloginfo('wpurl');?>/comment">
   <?php do_action('comment_form', $post->ID); ?>  
</form>

I have required fields on all inputs, but it is only working on the comment text field.

Read More

Any ideas?

Related posts

Leave a Reply

2 comments

  1. First of all, I highly recommend using the comment_form() function. It’s all of the above code in one line (more if you need to tweak parts of the code). You can designate name and email as required in the settings section of your admin area:

    Discussion settings

  2. (Note: I’m an experienced developer, but I’m new to the WP code base, so take the following in that context.)

    The .required CSS class seems to be used for styling purposes but not for logic. There are two other classes of possible interest — form-required and aria-required. form-required is specifically checked for in wp-includes/js/wp-ajax-response.dev.js (the minified version is what is referenced on pages) and is used for validation. However, the required fields in a comment (whose settings were pointed to by John Bloch) are explicitly checked for in wp-comments-post.php (near line 76) and there doesn’t seem to be a general mechanism for extending this. I.e. there’s not a loop saying something like foreach ($fields as $field) {if ($field.is_required() ...}.

    Requiring a field is part of the larger problem of form validation (ie. not only does it need to be non-blank, but its contents need to meet certain criteria), a subject which does not seem to be addressed in a general way in the WP core code.