wp_check_comment_data_max_lengths()
云策文档标注
概述
wp_check_comment_data_max_lengths() 函数用于检查评论数据是否超过最大字符限制。它接收评论数据数组作为参数,并返回 WP_Error 或 true。
关键要点
- 参数:$comment_data(数组,必需),包含插入评论的参数。
- 返回值:如果评论字段超出限制,返回 WP_Error;否则返回 true。
- 函数内部调用 wp_get_comment_fields_max_lengths() 获取最大长度限制。
- 检查的字段包括 comment_author、comment_author_email、comment_author_url 和 comment_content。
- 使用 mb_strlen 或 strlen 进行长度比较,超出限制时返回相应的 WP_Error 对象。
代码示例
function wp_check_comment_data_max_lengths( $comment_data ) {
$max_lengths = wp_get_comment_fields_max_lengths();
if ( isset( $comment_data['comment_author'] ) && mb_strlen( $comment_data['comment_author'], '8bit' ) > $max_lengths['comment_author'] ) {
return new WP_Error( 'comment_author_column_length', __( 'Error: Your name is too long.' ), 200 );
}
if ( isset( $comment_data['comment_author_email'] ) && strlen( $comment_data['comment_author_email'] ) > $max_lengths['comment_author_email'] ) {
return new WP_Error( 'comment_author_email_column_length', __( 'Error: Your email address is too long.' ), 200 );
}
if ( isset( $comment_data['comment_author_url'] ) && strlen( $comment_data['comment_author_url'] ) > $max_lengths['comment_author_url'] ) {
return new WP_Error( 'comment_author_url_column_length', __( 'Error: Your URL is too long.' ), 200 );
}
if ( isset( $comment_data['comment_content'] ) && mb_strlen( $comment_data['comment_content'], '8bit' ) > $max_lengths['comment_content'] ) {
return new WP_Error( 'comment_content_column_length', __( 'Error: Your comment is too long.' ), 200 );
}
return true;
}
原文内容
Compares the lengths of comment data against the maximum character limits.
Parameters
$comment_dataarrayrequired-
Array of arguments for inserting a comment.
Source
function wp_check_comment_data_max_lengths( $comment_data ) {
$max_lengths = wp_get_comment_fields_max_lengths();
if ( isset( $comment_data['comment_author'] ) && mb_strlen( $comment_data['comment_author'], '8bit' ) > $max_lengths['comment_author'] ) {
return new WP_Error( 'comment_author_column_length', __( '<strong>Error:</strong> Your name is too long.' ), 200 );
}
if ( isset( $comment_data['comment_author_email'] ) && strlen( $comment_data['comment_author_email'] ) > $max_lengths['comment_author_email'] ) {
return new WP_Error( 'comment_author_email_column_length', __( '<strong>Error:</strong> Your email address is too long.' ), 200 );
}
if ( isset( $comment_data['comment_author_url'] ) && strlen( $comment_data['comment_author_url'] ) > $max_lengths['comment_author_url'] ) {
return new WP_Error( 'comment_author_url_column_length', __( '<strong>Error:</strong> Your URL is too long.' ), 200 );
}
if ( isset( $comment_data['comment_content'] ) && mb_strlen( $comment_data['comment_content'], '8bit' ) > $max_lengths['comment_content'] ) {
return new WP_Error( 'comment_content_column_length', __( '<strong>Error:</strong> Your comment is too long.' ), 200 );
}
return true;
}
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |