返回介绍

wp_insert_comment()

发布于 2017-09-11 12:14:09 字数 6422 浏览 1049 评论 0 收藏 0

wp_insert_comment( array $commentdata )

Inserts a comment into the database.


description


参数

$commentdata

(array) (Required) Array of arguments for inserting a new comment.

  • 'comment_agent'
    (string) The HTTP user agent of the $comment_author when the comment was submitted. Default empty.
  • 'comment_approved'
    (int|string) Whether the comment has been approved. Default 1.
  • 'comment_author'
    (string) The name of the author of the comment. Default empty.
  • 'comment_author_email'
    (string) The email address of the $comment_author. Default empty.
  • 'comment_author_IP'
    (string) The IP address of the $comment_author. Default empty.
  • 'comment_author_url'
    (string) The URL address of the $comment_author. Default empty.
  • 'comment_content'
    (string) The content of the comment. Default empty.
  • 'comment_date'
    (string) The date the comment was submitted. To set the date manually, $comment_date_gmt must also be specified. Default is the current time.
  • 'comment_date_gmt'
    (string) The date the comment was submitted in the GMT timezone. Default is $comment_date in the site's GMT timezone.
  • 'comment_karma'
    (int) The karma of the comment. Default 0.
  • 'comment_parent'
    (int) ID of this comment's parent, if any. Default 0.
  • 'comment_post_ID'
    (int) ID of the post that relates to the comment, if any. Default 0.
  • 'comment_type'
    (string) Comment type. Default empty.
  • 'comment_meta'
    (array) Optional. Array of key/value pairs to be stored in commentmeta for the new comment.
  • 'user_id'
    (int) ID of the user who submitted the comment. Default 0.


返回值

(int|false) The new comment's ID on success, false on failure.


源代码

File: wp-includes/comment.php

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     = ! isset( $data['comment_type'] )     ? '' : $data['comment_type'];
	$comment_parent   = ! isset( $data['comment_parent'] )   ? 0  : $data['comment_parent'];

	$user_id  = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];

	$compacted = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', '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 ( $comment_approved == 1 ) {
		wp_update_comment_count( $comment_post_ID );

		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
			wp_cache_delete( "lastcommentmodified:$timezone", '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;
}

更新日志

Versiondescription
4.4.0Introduced $comment_meta argument.
2.0.0Introduced.

相关函数

Uses

  • wp-includes/cache.php: wp_cache_delete()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/formatting.php: get_gmt_from_date()
  • wp-includes/functions.php: current_time()
  • wp-includes/plugin.php: do_action()
  • wp-includes/wp-db.php: wpdb::insert()
  • wp-includes/comment.php: clean_comment_cache()
  • wp-includes/comment.php: wp_update_comment_count()
  • wp-includes/comment.php: wp_insert_comment
  • wp-includes/comment.php: add_comment_meta()
  • wp-includes/comment.php: get_comment()
  • Show 6 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::create_item()
  • wp-includes/comment.php: wp_new_comment()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文