钩子文档

wp_insert_attachment_data

💡 云策文档标注

概述

wp_insert_attachment_data 是一个 WordPress 过滤器钩子,用于在附件数据被插入或更新到数据库之前过滤处理后的数据。它允许开发者修改附件帖子的数据,提供多个参数以访问不同处理阶段的数据。

关键要点

  • 这是一个过滤器钩子,用于过滤附件帖子数据,在数据库操作前执行。
  • 参数包括:$data(已处理数据)、$postarr(已清理但未处理数据)、$unsanitized_postarr(原始未清理数据)和 $update(更新标志)。
  • 钩子从 WordPress 3.9.0 版本引入,后续版本增加了参数,如 6.0.0 添加了 $update 参数。
  • 常用于 wp_insert_post() 函数中,用于插入或更新帖子。

代码示例

$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr, $update );

注意事项

使用此钩子时,需注意参数的数据状态:$data 是已处理的数据,$postarr 是已清理但未处理的,$unsanitized_postarr 是原始未清理数据,可用于高级自定义处理。


📄 原文内容

Filters attachment post data before it is updated in or added to the database.

Parameters

$dataarray
An array of slashed, sanitized, and processed attachment post data.
$postarrarray
An array of slashed and sanitized attachment post data, but not processed.
$unsanitized_postarrarray
An array of slashed yet *unsanitized* and unprocessed attachment post data as originally passed to wp_insert_post() .
$updatebool
Whether this is an existing attachment post being updated.

Source

$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr, $update );

Changelog

Version Description
6.0.0 The $update parameter was added.
5.4.1 The $unsanitized_postarr parameter was added.
3.9.0 Introduced.