wp_insert_comment()
云策文档标注
概述
wp_insert_comment() 函数用于向 WordPress 数据库插入新评论,接受一个包含评论数据的数组参数,并返回新评论的 ID 或失败时返回 false。该函数处理评论的默认值、元数据存储,并在插入后触发相关 Hook。
关键要点
- 参数 $commentdata 为必需数组,包含评论作者、内容、日期、类型等字段,每个字段有默认值。
- 函数返回 int|false,成功时返回新评论 ID,失败时返回 false。
- 支持可选 comment_meta 参数,用于存储评论的元数据。
- 插入后触发 wp_insert_comment Hook,并更新评论计数和缓存。
- 注意 $comment_type 参数长度不能超过 20 个字符。
代码示例
function ic_wp_insert_comment( $field, $postId ) {
$current_user = wp_get_current_user();
if ( comments_open( $postId ) ) {
$data = array(
'comment_post_ID' => $postId,
'comment_content' => $field['comment'],
'comment_parent' => $field['comment_parent'],
'user_id' => $current_user->ID,
'comment_author' => $current_user->user_login,
'comment_author_email' => $current_user->user_email,
'comment_author_url' => $current_user->user_url,
'comment_meta' => array(
'property_district' => $field['property_district'],
'property_estate' => $field['property_estate'],
'custom_tag' => $field['custom_tag'],
)
);
$comment_id = wp_insert_comment( $data );
if ( ! is_wp_error( $comment_id ) ) {
return $comment_id;
}
}
return false;
}注意事项
- 手动设置 comment_date 时,必须同时指定 comment_date_gmt。
- $comment_type 参数默认值为 'comment',且长度限制为 20 个字符。
- 函数内部使用 wp_unslash() 处理数据,并调用 wpdb::insert() 插入数据库。
原文内容
Inserts a comment into the database.
Parameters
$commentdataarrayrequired-
Array of arguments for inserting a new comment.
comment_agentstringThe HTTP user agent of the$comment_authorwhen the comment was submitted. Default empty.comment_approvedint|stringWhether the comment has been approved. Default 1.comment_authorstringThe name of the author of the comment. Default empty.comment_author_emailstringThe email address of the$comment_author. Default empty.comment_author_IPstringThe IP address of the$comment_author. Default empty.comment_author_urlstringThe URL address of the$comment_author. Default empty.comment_contentstringThe content of the comment. Default empty.comment_datestringThe date the comment was submitted. To set the date manually,$comment_date_gmtmust also be specified.
Default is the current time.comment_date_gmtstringThe date the comment was submitted in the GMT timezone.
Default is$comment_datein the site’s GMT timezone.comment_karmaintThe karma of the comment. Default 0.comment_parentintID of this comment’s parent, if any. Default 0.comment_post_IDintID of the post that relates to the comment, if any.
Default 0.comment_typestringComment type. Default'comment'.comment_metaarrayOptional. Array of key/value pairs to be stored in commentmeta for the new comment.user_idintID of the user who submitted the comment. Default 0.
Source
function wp_insert_comment( $commentdata ) {
global $wpdb;
$data = wp_unslash( $commentdata );
$comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author'];
$comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
$comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url'];
$comment_author_ip = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP'];
$comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date'];
$comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
$comment_post_id = ! isset( $data['comment_post_ID'] ) ? 0 : $data['comment_post_ID'];
$comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content'];
$comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma'];
$comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];
$comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];
$comment_type = empty( $data['comment_type'] ) ? 'comment' : $data['comment_type'];
$comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent'];
$user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
$compacted = array(
'comment_post_ID' => $comment_post_id,
'comment_author_IP' => $comment_author_ip,
);
$compacted += compact(
'comment_author',
'comment_author_email',
'comment_author_url',
'comment_date',
'comment_date_gmt',
'comment_content',
'comment_karma',
'comment_approved',
'comment_agent',
'comment_type',
'comment_parent',
'user_id'
);
if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
return false;
}
$id = (int) $wpdb->insert_id;
if ( 1 === (int) $comment_approved ) {
wp_update_comment_count( $comment_post_id );
$data = array();
foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
$data[] = "lastcommentmodified:$timezone";
}
wp_cache_delete_multiple( $data, 'timeinfo' );
}
clean_comment_cache( $id );
$comment = get_comment( $id );
// If metadata is provided, store it.
if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) {
foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) {
add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true );
}
}
/**
* Fires immediately after a comment is inserted into the database.
*
* @since 2.8.0
*
* @param int $id The comment ID.
* @param WP_Comment $comment Comment object.
*/
do_action( 'wp_insert_comment', $id, $comment );
return $id;
}
Hooks
- do_action( ‘wp_insert_comment’, int $id, WP_Comment $comment )
-
Fires immediately after a comment is inserted into the database.
Skip to note 3 content
Mehedi Foysal
function ic_wp_insert_comment( $field, $postId ) { $current_user = wp_get_current_user(); if ( comments_open( $postId ) ) { $data = array( 'comment_post_ID' => $postId, 'comment_content' => $field['comment'], 'comment_parent' => $field['comment_parent'], 'user_id' => $current_user->ID, 'comment_author' => $current_user->user_login, 'comment_author_email' => $current_user->user_email, 'comment_author_url' => $current_user->user_url, 'comment_meta' => array( 'property_district' => $field['property_district'], 'property_estate' => $field['property_estate'], 'custom_tag' => $field['custom_tag'], ) ); $comment_id = wp_insert_comment( $data ); if ( ! is_wp_error( $comment_id ) ) { return $comment_id; } } return false; }Skip to note 4 content
meta4
Note that the $comment_type argument cannot be longer than 20 characters.