• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Programming / WordPress Allow Empty Comment

WordPress Allow Empty Comment

Last updated on June 10, 2016 by Sal Ferrarello

This question came up recently, “can you allow a WP comment to be submitted without a comment body (i.e. only comment meta)?”

Why I Couldn’t Solve this on the Server Side

Unfortunately, I was unable to solve this on the server side.  WordPress comments are posted to wp-comments-post.php, which calls the function wp_handle_comment_submission() before any filters are applied.

In wp_handle_comment_submission(), the following check occurs before any filters (e.g. preprocess_comment) are applied.

if ( '' == $comment_content ) {
    return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
}

Perhaps someone else has insight on how this could still be solved on the server side but I was unable to determine a server side solution. If you have an idea, let me know in the comments.

How I Solved this on the Client Side (with jQuery/JavaScript)

The following code listens for a submit to occur on the comment form. When it does occur:

  • We check if the comment body is empty (and only proceed if it is)
  • We set the comment body text color to white (to prevent our auto-populated text from flashing on the screen)
  • We auto-populate the comment body with 'Automated Comment: ' followed by the number of seconds since January 1, 1970 (Epoch Time a.k.a. Unix Time)
    • by starting each comment body with the same string Automated Comment: it makes it easy for us to detect an automated comment on display
    • by using the Epoch Time as part of the comment we avoid the WordPress check for duplicate comments
jQuery('#commentform').on('submit', function() {
var $comment = jQuery('#comment');
if ( '' === $comment.val().trim() ) {
$comment
.css({'color':'#fff'})
.val( 'Automated Comment: ' + Math.floor( new Date().getTime() / 1000 ) );
}
});
view raw populate-blank-comment.js hosted with ❤ by GitHub
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Programming Tagged With: comments, WordPress

Reader Interactions

Comments

  1. Jeremy Pry says

    September 20, 2016 at 6:03 pm

    I have an idea for how you could solve this problem server-side, although it’s admittedly more involved than your simple Javascript solution.

    First, apply a filter to comment_form_defaults. Update the action value in that array to point to a different page on the site. That new page should take over the processing of a new comment in a similar manner to wp-comments-post.php. Then you can do any processing you want before passing the data off to wp_handle_comment_submission().

    Of course, you can also submit a Trac ticket to request a filter 🙂

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in