I have a comment form and in that comment form I've added an extra input field called with the name 'review_title'. When one submits the comment form the comment it's gets inserted perfectly fine and the extra field gets inserted into the comment meta. Want I want to achieve is a server side check when the comment is submitted, that checks if the extra field is empty and if the extra field is empty, then stop the script from posting the comment. Here is my attempt:
This is the form:
<form action="http://localhost/test/wp-comments-post.php" method="post" id="commentform" class="comment-form">
<p class="comment-form-title">
<label for="pmg_comment_title">Review Title</label>
<input type="text" name="review_title" id="pmg_comment_title" />
</p>
<p class="comment-form-comment">
<label for="comment">Comment</label>
<textarea id="comment" name="comment" required="required"></textarea>
</p>
<input name="submit" type="submit" id="submit" value="Post Comment" />
</form>
This is the function I tried to do this with:
function verify_comment_meta_data( $commentdata ) {
if (!isset($_POST['review_title']))
wp_die( __( 'Error: please fill the required field (city).' ) );
return $commentdata;
}
add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
Unfortunately this doesn't do anything at all, if I leave the 'review_title' field blank, it'll still post the comment just without that field.
So my question is how to stop the comment submit, if the 'review_title' field is empty?