函数文档

wp_save_post_revision_on_insert()

💡 云策文档标注

概述

wp_save_post_revision_on_insert() 是一个 WordPress 函数,用于在文章插入后保存修订版本。它主要处理更新操作,确保在文章更新时触发修订保存。

关键要点

  • 函数在文章插入后调用,但仅当更新现有文章时才执行修订保存。
  • 它检查是否有 post_updated 钩子注册了 wp_save_post_revision 动作,以避免重复保存。
  • 函数内部调用 wp_save_post_revision() 来创建当前文章版本的修订。

代码示例

function wp_save_post_revision_on_insert( $post_id, $post, $update ) {
    if ( ! $update ) {
        return;
    }

    if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) {
        return;
    }

    wp_save_post_revision( $post_id );
}

注意事项

  • 函数参数包括 $post_id(整数,必需)、$post(WP_Post 对象,必需)和 $update(布尔值,必需),用于判断是否为更新操作。
  • 相关函数:has_action() 用于检查钩子动作,wp_save_post_revision() 用于实际保存修订。
  • 此函数从 WordPress 6.4.0 版本开始引入。

📄 原文内容

Saves revisions for a post after all changes have been made.

Parameters

$post_idintrequired
The post id that was inserted.
$postWP_Postrequired
The post object that was inserted.
$updateboolrequired
Whether this insert is updating an existing post.

Source

function wp_save_post_revision_on_insert( $post_id, $post, $update ) {
	if ( ! $update ) {
		return;
	}

	if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) {
		return;
	}

	wp_save_post_revision( $post_id );
}

Changelog

Version Description
6.4.0 Introduced.