钩子文档

rest_pre_insert_comment

💡 云策文档标注

概述

rest_pre_insert_comment 是一个 WordPress REST API 钩子,用于在通过 wp_insert_comment() 插入评论前过滤评论数据。开发者可以通过此钩子修改评论数据或返回 WP_Error 来中断插入过程。

关键要点

  • 此钩子在评论通过 REST API 插入前被调用,允许修改 $prepared_comment 数据。
  • 返回 WP_Error 可以短路径插入,跳过后续处理。
  • 参数包括 $prepared_comment(数组或 WP_Error)和 $request(WP_REST_Request 对象)。
  • 从 4.8.0 版本起,$prepared_comment 可以是 WP_Error 以支持短路径功能。

代码示例

$prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );

📄 原文内容

Filters a comment before it is inserted via the REST API.

Description

Allows modification of the comment right before it is inserted via wp_insert_comment() .
Returning a WP_Error value from the filter will short-circuit insertion and allow skipping further processing.

Parameters

$prepared_commentarray|WP_Error
The prepared comment data for wp_insert_comment() .
$requestWP_REST_Request
Request used to insert the comment.

Source

$prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );

Changelog

Version Description
4.8.0 $prepared_comment can now be a WP_Error to short-circuit insertion.
4.7.0 Introduced.