Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How To Make Comment Message Field Not Required? WordPress

  • SOLVED

Hi,

For a specific project, it requires the functionality to have the comments field for the comments.php template optional and not a required field.

Is there a functions.php snippet code possible to disable this field to be required?



Answers (3)

2011-02-27

Ivaylo Draganov answers:

Hello,

A simple hack would be to filter the comment form textarea to include a space character. Thus even if the user doesn't type anything it will still validate. The drawback is that the user is in control and could possibly delete that space character. Here's the code anyway:

// filter comment form defaults
add_filter('comment_form_defaults', 'my_comment_form_defaults');

function my_comment_form_defaults($defaults) {

// change the textarea to include an empty space, thus validating it even without user input
$defaults['comment_field'] = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">&nbsp;</textarea></p>';

return $defaults;

}


There sure is a better way to accomplish that but I'd have to dig in deeper.


Ivaylo Draganov comments:

Hm, seems like there's no easy way to do it without hacking the core. And as we all know that is not the WP way... but if it doesn't bother you, comment out lines 83 and 84 in <em>wp-comments-post.php</em>(this file is in the WP root).

if ( '' == $comment_content )
wp_die( __('Error: please type a comment.') );


I'm not proficient in PHP so that's my stab at the problem. Another thing you could do is to use JavaScript form validation to add in the blank character (or whatever) to the textarea on form submission.

2011-02-27

Sébastien | French WordpressDesigner answers:

uncheck the box "Comment author must fill out name and e-mail "
in settings>discussion


Sébastien | French WordpressDesigner comments:

Euh that's a simple solution nô ?

2011-02-27

Hameedullah Khan answers:

There is no way Wordpress will store an empty comment in DB. Even if you will add space it will be stripped out. If your target is just to get the comment poster info stored in the db, you can do it by setting "N/A" or any other thing which will represent comment was not provided. You can use the below code snippet in your functions.php to accomplish this.

add_action('pre_comment_on_post', 'dump_comment');
function dump_comment($post_id, $author=null, $email=null) {
$comment = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
if (!$comment) {
$_POST['comment'] = "N/A";
}
}