钩子文档

rest_insert_attachment

💡 云策文档标注

概述

rest_insert_attachment 是一个 WordPress REST API 钩子,在通过 REST API 创建或更新单个附件后触发。它提供附件对象、请求对象和创建标志作为参数,便于开发者执行自定义操作。

关键要点

  • 触发时机:在通过 REST API 成功创建或更新单个附件后立即触发。
  • 参数:$attachment(WP_Post 对象,表示插入或更新的附件)、$request(WP_REST_Request 对象,表示发送到 API 的请求)、$creating(布尔值,创建时为 true,更新时为 false)。
  • 用途:常用于在附件创建或更新后执行自定义逻辑,如处理元数据或触发其他操作。
  • 相关函数:由 WP_REST_Attachments_Controller::insert_attachment() 调用,用于插入附件到数据库但不更新附件元数据。
  • 版本历史:从 WordPress 4.7.0 版本开始引入。

📄 原文内容

Fires after a single attachment is created or updated via the REST API.

Parameters

$attachmentWP_Post
Inserted or updated attachment object.
$requestWP_REST_Request
The request sent to the API.
$creatingbool
True when creating an attachment, false when updating.

Source

do_action( 'rest_insert_attachment', $attachment, $request, true );

Changelog

Version Description
4.7.0 Introduced.