str_word_count()
is not a perfect function so there are chance it could fail but for a simple validation I think it will work in this case. Finally, there’s a back button courtesy of bonger from WordPress Stack Exchange.
/**
* Additional comment validation
*
* @param Integer $post_id - The ID of the given Post user is commenting on.
*
* @return void
*/
function mcgee_comment_validation( $post_id ) {
if( ! ( isset( $_POST, $_POST['comment'] ) && ! empty( $_POST['comment'] ) ) ) {
return;
}
$minimum_count = 100;
$content = $_POST['comment'];
$back_link = sprintf( __( '<br /><a href="javascript:history.go(-1)">Back To %1$s</a>' ), get_the_title( $post_id ) );
if( str_word_count( $content ) < $minimum_count ) {
wp_die( __( 'Comments have a minimum length of 100 words.' ) . $back_link );
}
}
add_action( 'pre_comment_on_post', 'mcgee_comment_validation' );
The above you could add as an MU Plugin, Normal Plugin, or add straight to your themes functions.php
file ( assuming it’s a child theme or non-updatable theme ).
Sorry I haven’t replied sooner, I just found the email notification of a reply in my spam folder!
I have just added it into the themes functions.php file at the bottom and it seems to be working perfectly! Thanks a lot for taking the time to figure this one out, it’s a great help.
]]>