钩子文档

wp_insert_post_data

💡 云策文档标注

概述

wp_insert_post_data 是一个 WordPress 过滤器,用于在文章数据插入数据库前修改已处理的数据。它接收多个参数,包括已处理、未处理的数据和更新状态,允许开发者自定义文章内容。

关键要点

  • 过滤器在文章数据插入数据库前触发,可修改 $data 数组中的字段。
  • 参数包括 $data(已处理数据)、$postarr(已处理但未修改数据)、$unsanitized_postarr(未处理原始数据)和 $update(更新标志)。
  • 使用 add_filter() 时,需设置 $accepted_args 为 2 以访问 $postarr 参数。
  • 可通过 $postarr['ID'] 获取文章 ID,但需注意在特定情况下可能不可用。
  • 过滤器也适用于文章更新和删除操作,不限于插入。

代码示例

// 移除文章标题中的 HTML 实体
function filter_post_name( $data, $postarr, $unsanitized_postarr) {
    $data['post_title'] = html_entity_decode($data['post_title']);
    return $data;
}

add_filter( 'wp_insert_post_data', 'filter_post_name', 10, 3);

注意事项

  • 确保正确处理参数,避免数据冲突或安全风险。
  • 注意 $data 和 $postarr 的默认字段差异,如 $data 包含更多标准字段。
  • 在修改数据时,考虑更新和删除场景的影响。

📄 原文内容

Filters slashed post data just before it is inserted into the database.

Parameters

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

More Information

You must pass the value 2 for the $accepted_args argument in add_filter() if you want to access $postarr.

Some have problems to get the post ID inside wp_insert_post_data:

If you have access to $postarr, you can easily retrieve the post ID with

$my_post_id = $postarr['ID'];

The defaults for the parameter $data are:
<br>
'post_author',<br>
'post_date',<br>
'post_date_gmt',<br>
'post_content',<br>
'post_content_filtered',<br>
'post_title',<br>
'post_excerpt',<br>
'post_status',<br>
'post_type',<br>
'comment_status',<br>
'ping_status',<br>
'post_password',<br>
'post_name',<br>
'to_ping',<br>
'pinged',<br>
'post_modified',<br>
'post_modified_gmt',<br>
'post_parent',<br>
'menu_order',<br>
'guid'<br>

The defaults for the parameter $postarr are:
<br>
'post_status' - Default is 'draft'.<br>
'post_type' - Default is 'post'.<br>
'post_author' - Default is current user ID ($user_ID). The ID of the user who added the post.<br>
'ping_status' - Default is the value in 'default_ping_status' option.<br>
Whether the attachment can accept pings.<br>
'post_parent' - Default is 0. Set this for the post it belongs to, if any.<br>
'menu_order' - Default is 0. The order it is displayed.<br>
'to_ping' - Whether to ping.<br>
'pinged' - Default is empty string.<br>
'post_password' - Default is empty string. The password to access the attachment.<br>
'guid' - Global Unique ID for referencing the attachment.<br>
'post_content_filtered' - Post content filtered.<br>
'post_excerpt' - Post excerpt.<br>

The post $postarr looks like:
<br>
'post_status',<br>
'post_type',<br>
'post_author',<br>
'ping_status',<br>
'post_parent',<br>
'menu_order',<br>
'to_ping',<br>
'pinged',<br>
'post_password',<br>
'guid',<br>
'post_content_filtered',<br>
'post_excerpt',<br>
'import_id',<br>
'post_content',<br>
'post_title',<br>
'ID',<br>
'post_date',<br>
'post_date_gmt',<br>
'comment_status',<br>
'post_name',<br>
'post_modified',<br>
'post_modified_gmt',<br>
'post_mime_type',<br>
'comment_count',<br>
'ancestors',<br>
'post_category',<br>
'tags_input',<br>
'filter'<br>

Source

$data = apply_filters( 'wp_insert_post_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.
2.7.0 Introduced.

User Contributed Notes