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
- by starting each comment body with the same string
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 ) ); | |
} | |
}); |
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 theaction
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 towp-comments-post.php
. Then you can do any processing you want before passing the data off towp_handle_comment_submission()
.Of course, you can also submit a Trac ticket to request a filter 🙂