25% off ProSNIPC25OFF

Set Min/Max Comment Length

Rejects comment submissions that are shorter than a minimum or longer than a maximum character count.

PHPby SnipCraft
php
<?php
add_filter( 'preprocess_comment', 'scseed_enforce_comment_length' );
if ( ! function_exists( 'scseed_enforce_comment_length' ) ) {
    function scseed_enforce_comment_length( $data ) {
        $min_length = 20;   // Minimum characters required.
        $max_length = 2000; // Maximum characters allowed.
        $len = mb_strlen( trim( $data['comment_content'] ) );

        if ( $len < $min_length ) {
            wp_die(
                sprintf(
                    /* translators: %d: minimum comment length */
                    esc_html__( 'Your comment is too short. Please write at least %d characters.', 'scseed' ),
                    $min_length
                ),
                esc_html__( 'Comment Too Short', 'scseed' ),
                [ 'back_link' => true, 'response' => 400 ]
            );
        }

        if ( $len > $max_length ) {
            wp_die(
                sprintf(
                    /* translators: %d: maximum comment length */
                    esc_html__( 'Your comment is too long. Please limit it to %d characters.', 'scseed' ),
                    $max_length
                ),
                esc_html__( 'Comment Too Long', 'scseed' ),
                [ 'back_link' => true, 'response' => 400 ]
            );
        }

        return $data;
    }
}
#comments#content#moderation#quality#spam