wp_delete_comment()删除一个评论

目录

描述

译文

删除评论编号,之后可能更新文章评论总数。

如果评论被许可并具有相应可用的文章编号,文章评论总数将被更新。

原文

Trashes or deletes a comment.

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.

wp_delete_comment() 描述

用法

<?php wp_delete_comment$comment_id$force_delete ?>

wp_delete_comment() 用法

参数

$comment_id

(integer) (必填) Comment ID

默认值: None

$force_delete

(boolean) (可选) Move comment to trash or delete.

默认值: false

wp_delete_comment() 参数

返回值

(boolean) 

False if delete comment query failure, true on success.

wp_delete_comment() 返回值

注意

wp_delete_comment() 注意

历史

添加于 版本: 2.0.0

wp_delete_comment() 历史

源文件

wp_delete_comment() 函数的代码位于 wp-includes/comment.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Trashes or deletes a comment.
 *
 * 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.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $comment_id Comment ID
 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
 * @return bool True on success, false on failure.
 */
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_id), 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_id );

	// Move children up a level.
	$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
	if ( !empty($children) ) {
		$wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $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_id ) );
	foreach ( $meta_ids as $mid )
		delete_metadata_by_mid( 'comment', $mid );

	if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $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_id );

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

	clean_comment_cache($comment_id);

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

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

wp_delete_comment() 源文件

相关