返回介绍

wp_delete_comment()

发布于 2017-09-11 11:49:54 字数 4753 浏览 1114 评论 0 收藏 0

wp_delete_comment( int|WP_Comment $comment_id,  bool $force_delete = false )

Trashes or deletes a comment.


description

The comment is moved to trash instead of permanently deleted unless trash is disabled, item is already in the trash, or $force_delete is true.

The post comment count will be updated if the comment was approved and has a post ID available.


参数

$comment_id

(int|WP_Comment) (Required) Comment ID or WP_Comment object.

$force_delete

(bool) (Optional) Whether to bypass trash and force deletion. Default is false.

Default value: false


返回值

(bool) True on success, false on failure.


源代码

File: wp-includes/comment.php

function wp_delete_comment($comment_id, $force_delete = false) {
	global $wpdb;
	if (!$comment = get_comment($comment_id))
		return false;

	if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) )
		return wp_trash_comment($comment_id);

	/**
	 * Fires immediately before a comment is deleted from the database.
	 *
	 * @since 1.2.0
	 *
	 * @param int $comment_id The comment ID.
	 */
	do_action( 'delete_comment', $comment->comment_ID );

	// Move children up a level.
	$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID) );
	if ( !empty($children) ) {
		$wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment->comment_ID));
		clean_comment_cache($children);
	}

	// Delete metadata
	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
	foreach ( $meta_ids as $mid )
		delete_metadata_by_mid( 'comment', $mid );

	if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) )
		return false;

	/**
	 * Fires immediately after a comment is deleted from the database.
	 *
	 * @since 2.9.0
	 *
	 * @param int $comment_id The comment ID.
	 */
	do_action( 'deleted_comment', $comment->comment_ID );

	$post_id = $comment->comment_post_ID;
	if ( $post_id && $comment->comment_approved == 1 )
		wp_update_comment_count($post_id);

	clean_comment_cache( $comment->comment_ID );

	/** This action is documented in wp-includes/comment.php */
	do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' );

	wp_transition_comment_status('delete', $comment->comment_approved, $comment);
	return true;
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/comment.php: wp_set_comment_status
  • wp-includes/plugin.php: do_action()
  • wp-includes/wp-db.php: wpdb::get_col()
  • wp-includes/wp-db.php: wpdb::update()
  • wp-includes/wp-db.php: wpdb::delete()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/comment.php: clean_comment_cache()
  • wp-includes/comment.php: wp_update_comment_count()
  • wp-includes/comment.php: wp_get_comment_status()
  • wp-includes/comment.php: wp_transition_comment_status()
  • wp-includes/comment.php: wp_trash_comment()
  • wp-includes/comment.php: delete_comment
  • wp-includes/comment.php: deleted_comment
  • wp-includes/comment.php: get_comment()
  • wp-includes/meta.php: delete_metadata_by_mid()
  • Show 10 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::delete_item()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_comment()
  • wp-includes/functions.php: wp_scheduled_delete()
  • wp-includes/post.php: wp_delete_attachment()
  • wp-includes/post.php: wp_delete_post()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_deleteComment()
  • wp-includes/comment.php: wp_trash_comment()
  • Show 2 more used by Hide more used by

User Contributed Notes

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

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

发布评论

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