How to make comment text field un-required?

All we need is an easy explanation of the problem, so here it is.

My requirements are bit different I was able to hide email and website field with filter but what I want to know is there a way to make comment text un-required when I am giving comment without comment text area filled its giving me error that comment is required please guide me how i can make comment text unrequired.

       add_action('pre_comment_on_post', 'dump_comment');
       function dump_comment($post_id, $author=null,     $email=null) {
       $comcnt = $cmntcount = comments_number( '#0', '#1', '#%' );
       $comment = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
       if (!$comment) {
       $_POST['comment'] = 'Design' . $comcnt;

        }
        }

Updated and working code.

      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'] = 'Design #' . get_comments_number();

       }
        }

making the comment unique everytime. by adding the value of comment count.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can circumvent the check for empty comments easily by adding the uploaded image as HTML to the comment text:

add_action( 'pre_comment_on_post', 'allow_empty_comment_text' );

function allow_empty_comment_text( $text = '' )
{
    if ( ! isset ( $_POST['comment'] ) or '' === trim( $_POST['comment'] ) )
    {
        $img = '/* Process uploaded image here, create an <img> tag. */'    
        $_POST['comment'] = '<img>'; // use a real img tag here
    }
}

Method 2

function rei_preprocess_comment($comment_data) {
    if ($comment_data['comment_content'] == '%dummy-text%') {
        $comment_data['comment_content'] = ''; // replace dummy text.
    }
    return $comment_data;
}
add_filter('preprocess_comment', 'rei_preprocess_comment');
function rei_wp_footer() {
    ?>
    <script>
    jQuery(function($){
        var comment = $('textarea#comment');
        comment.removeAttr('required'); // remove required attribute of textarea.
        $('#commentform').on('submit',function(){
            if (comment.val() == '') {
                comment.css('text-indent','-999px').val('%dummy-text%'); // change to dummy text.
            }
        });
    });
    </script>
    <?php
}
add_action( 'wp_footer', 'rei_wp_footer' );

Method 3

This function allows the user to submit a comment without any text in the textarea. Then all you need to do is hide the comment textarea with some display:none; css.

function rei_preprocess_comment($comment_data) {
    if ($comment_data['comment_content'] == '%dummy-text%') {
        $comment_data['comment_content'] = ''; // replace dummy text.
    }
    return $comment_data;
}
add_filter('preprocess_comment', 'rei_preprocess_comment');
function rei_wp_footer() {
    ?>
    <script>
    jQuery(function($){
        var comment = $('textarea#comment');
        comment.removeAttr('required'); // remove required attribute of textarea.
        $('#commentform').on('submit',function(){
            if (comment.val() == '') {
                comment.css('text-indent','-999px').val('%dummy-text%'); // change to dummy text.
            }
        });
    });
    </script>
    <?php
}
add_action( 'wp_footer', 'rei_wp_footer' );

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply