trash_comment
云策文档标注
概述
trash_comment 是一个 WordPress 动作钩子,在评论被移至垃圾箱前立即触发。它允许开发者在评论被删除前执行自定义代码,例如记录日志或进行数据验证。
关键要点
- trash_comment 钩子在评论被移至垃圾箱前触发,提供 $comment_id 和 $comment 参数。
- 参数包括 $comment_id(评论 ID 的字符串)和 $comment(WP_Comment 对象)。
- 此钩子从 WordPress 2.9.0 版本引入,并在 4.9.0 版本添加了 $comment 参数。
- 相关函数 wp_trash_comment() 用于将评论移至垃圾箱。
代码示例
function trash_comment_02092021( $comment_id, $comment ) {
// do the code here
error_log($comment_id);
}
add_action( 'trash_comment', 'trash_comment_02092021', 10, 2 );注意事项
- 确保钩子函数正确接收两个参数,以避免错误。
- 此钩子仅适用于评论被移至垃圾箱的操作,不适用于其他评论状态变更。
原文内容
Fires immediately before a comment is sent to the Trash.
Parameters
$comment_idstring-
The comment ID as a numeric string.
$commentWP_Comment-
The comment to be trashed.
Source
do_action( 'trash_comment', $comment->comment_ID, $comment );
Skip to note 2 content
hasanrang05
function trash_comment_02092021( $comment_id, $comment ) { // do the code here error_log($comment_id); } add_action( 'spam_comment', 'trash_comment_02092021', 10, 2 );