函数文档

clean_comment_cache()

💡 云策文档标注

概述

clean_comment_cache() 函数用于从对象缓存中移除一个或多个评论。它通过 wp_cache_delete_multiple() 删除缓存项,并触发 clean_comment_cache 钩子以允许开发者执行自定义操作。

关键要点

  • 参数 $ids 接受整数或数组,指定要移除的评论 ID。
  • 函数内部调用 wp_cache_delete_multiple() 批量删除缓存,并触发 clean_comment_cache 钩子。
  • 相关函数包括 wp_cache_set_comments_last_changed() 用于更新评论缓存组的最后更改时间。
  • 该函数被多个核心函数调用,如 wp_delete_comment() 和 wp_update_comment(),以确保缓存一致性。

代码示例

function clean_comment_cache( $ids ) {
    $comment_ids = (array) $ids;
    wp_cache_delete_multiple( $comment_ids, 'comment' );
    foreach ( $comment_ids as $id ) {
        do_action( 'clean_comment_cache', $id );
    }
    wp_cache_set_comments_last_changed();
}

注意事项

  • clean_comment_cache 钩子在每个评论 ID 被移除后立即触发,可用于自定义缓存清理逻辑。
  • 函数自 WordPress 2.3.0 版本引入,是评论缓存管理的基础工具。

📄 原文内容

Removes a comment from the object cache.

Parameters

$idsint|arrayrequired
Comment ID or an array of comment IDs to remove from cache.

Source

function clean_comment_cache( $ids ) {
	$comment_ids = (array) $ids;
	wp_cache_delete_multiple( $comment_ids, 'comment' );
	foreach ( $comment_ids as $id ) {
		/**
		 * Fires immediately after a comment has been removed from the object cache.
		 *
		 * @since 4.5.0
		 *
		 * @param int $id Comment ID.
		 */
		do_action( 'clean_comment_cache', $id );
	}

	wp_cache_set_comments_last_changed();
}

Hooks

do_action( ‘clean_comment_cache’, int $id )

Fires immediately after a comment has been removed from the object cache.

Changelog

Version Description
2.3.0 Introduced.