Skip to content

Instantly share code, notes, and snippets.

@someguy9
Created April 18, 2024 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save someguy9/e4a0f60227d9fdeb95715b56ec003460 to your computer and use it in GitHub Desktop.
Save someguy9/e4a0f60227d9fdeb95715b56ec003460 to your computer and use it in GitHub Desktop.
Limit comment length in WordPress
<?php
// Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress
add_filter( 'preprocess_comment', 'smartwp_limit_comment_length' );
function smartwp_limit_comment_length( $comment ) {
// Limit the comments to 6000 characters
if ( strlen( $comment['comment_content'] ) > 6000 ) {
wp_die('Comment is too long. Comments must be under 6000 characters.');
}
// Require 50 characters to leave a comment
if ( strlen( $comment['comment_content'] ) < 50 ) {
wp_die('Comment is too short. Comments must be at least 50 characters.');
}
return $comment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment