Restrict characters in comment section

Never edit the WordPress core files.

Instead you should hook into the pre_comment_content, which is where the comment text from the textarea is being sanitized, before it’s inserted into the database.

In the example below preg_replace is used to sanitize the comment text submitted. You should modify the function to fit your needs.

function keha_filter_comment( $comment_content ) {
    $comment_sanitized = preg_replace( "/[^0-9a-zA-Z ]/", "", $comment_content );
    return $comment_sanitzed;
}
add_action( 'pre_comment_content', 'keha_filter_comment' );

There are also some other filter hooks available for the other comment fields available, you can find them within the WordPress code, look for the apply_filters, also listed below:

  • pre_comment_author_name
  • pre_comment_user_ip
  • pre_comment_author_url
  • pre_comment_author_email
  • pre_comment_content