Skip to content

Instantly share code, notes, and snippets.

@someguy9
Created July 29, 2024 15:16
Show Gist options
  • Save someguy9/be8e2664e39f97a0ae8d6e1b038e03f0 to your computer and use it in GitHub Desktop.
Save someguy9/be8e2664e39f97a0ae8d6e1b038e03f0 to your computer and use it in GitHub Desktop.
Limit comment length in WordPress by post ID
<?php
// Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress for a specific post
add_filter('preprocess_comment', 'smartwp_limit_comment_length');
function smartwp_limit_comment_length($comment) {
// Specify the post ID where you want this function to run
$specific_post_id = 123; // Replace 123 with your desired post ID
// Check if the current post is the specific post we want to target
if (get_the_ID() == $specific_post_id) {
// 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