wppaste
WordPress

wp_save_post_revision( int $post_id ): int|WP_Error|void

Since
2.6.0
Source
wp-includes/revision.php:130
Creates a revision for the current version of a post.

Description

Typically used immediately after a post update, as every update is a revision, and the most recent revision always matches the current post.

Compatibility

WordPress
since 2.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_idint
The ID of the post to save as a revision.

Return value

int|WP_Error|void
Void or 0 if error, new revision ID, if success.

Performance profile

How much work a call to wp_save_post_revision() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–124

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 144.

Plugin surface
3 hooks

Third-party callbacks on 'wp_save_post_revision_check_for_changes', 'wp_save_post_revision_post_has_changed', 'wp_save_post_revision_revisions_before_deletion' run inside this call, and their cost is not bounded by anything here.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 36 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_save_post_revision() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
defined('DOING_AUTOSAVE')6none
!doing_action()13–15doing_action(), get_post()
doing_action() && has_action()13–15doing_action(), has_action()
doing_action() && !has_action()18–20doing_action(), has_action(), get_post()
!doing_action()20–25doing_action(), get_post(), post_type_supports()
doing_action() && !has_action()25–30doing_action(), has_action(), get_post(), post_type_supports()
!doing_action() && post_type_supports() && !wp_revisions_enabled()27–29doing_action(), get_post(), post_type_supports(), wp_revisions_enabled()
doing_action() && !has_action() && post_type_supports() && !wp_revisions_enabled()32–34doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled()
!doing_action() && post_type_supports() && wp_revisions_enabled() && $revisions_to_keep42–55doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), _wp_put_post_revision(), wp_revisions_to_keep()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && $revisions_to_keep47–60doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), _wp_put_post_revision(), wp_revisions_to_keep()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && !apply_filters() && $revisions_to_keep53–62doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep()
!doing_action() && post_type_supports() && wp_revisions_enabled() && !$revisions_to_keep && $delete57–70doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
24 further outcomes, up to 124 instructions
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && !apply_filters() && $revisions_to_keep58–67doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters()61–71doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && !$revisions_to_keep && $delete62–75doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters()66–76doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters()
!doing_action() && post_type_supports() && wp_revisions_enabled() && !$revisions_to_keep && !$delete && !isset($revisions[$i])67–80doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && !apply_filters() && !$revisions_to_keep && $delete68–77doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && $revisions_to_keep71–81doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && !$revisions_to_keep && !$delete && !isset($revisions[$i])72–85doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && !apply_filters() && !$revisions_to_keep && $delete73–82doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys()75–84doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && $revisions_to_keep76–86doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && !apply_filters() && !$revisions_to_keep && !$delete && !isset($revisions[$i])78–87doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys()80–89doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && !apply_filters() && !$revisions_to_keep && !$delete && !isset($revisions[$i])83–92doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys() && $revisions_to_keep85–94doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !$revisions_to_keep && $delete86–96doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys() && $revisions_to_keep90–99doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !$revisions_to_keep && $delete91–101doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !$revisions_to_keep && !$delete && !isset($revisions[$i])96–106doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys() && !$revisions_to_keep && $delete100–109doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !$revisions_to_keep && !$delete && !isset($revisions[$i])101–111doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys() && !$revisions_to_keep && $delete105–114doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters()
!doing_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys() && !$revisions_to_keep && !$delete && !isset($revisions[$i])110–119doing_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()
doing_action() && !has_action() && post_type_supports() && wp_revisions_enabled() && isset($latest_revision) && apply_filters() && !array_keys() && !$revisions_to_keep && !$delete && !isset($revisions[$i])115–124doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled(), wp_get_post_revisions(), apply_filters(), _wp_post_revision_fields(), array_keys(), normalize_whitespace(), normalize_whitespace(), apply_filters(), _wp_put_post_revision(), wp_revisions_to_keep(), wp_get_post_revisions(), apply_filters(), array_slice()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1446–12422
8.51446–12422
8.41446–124226 fewer instructions than PHP 8.3
8.31506–12722
8.21506–12722
8.11506–127222 fewer instructions than PHP 7.4
7.41526–12822

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while wp_save_post_revision() runs, in this order:

  1. apply_filters( wp_save_post_revision_check_for_changes )filterline 186 (+56 into the body)

    Filters whether the post has changed since the latest revision.

  2. apply_filters( wp_save_post_revision_post_has_changed )filterline 208 (+78 into the body)

    Filters whether a post has changed.

  3. apply_filters( wp_save_post_revision_revisions_before_deletion )filterline 239 (+109 into the body)

    Filters the revisions to be considered for deletion.

Uses · 13

Show all 13

Used by · 3

Source code

function wp_save_post_revision( $post_id ) {	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {		return;	} 	// Prevent saving post revisions if revisions should be saved on wp_after_insert_post.	if ( doing_action( 'post_updated' ) && has_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert' ) ) {		return;	} 	$post = get_post( $post_id ); 	if ( ! $post ) {		return;	} 	if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {		return;	} 	if ( 'auto-draft' === $post->post_status ) {		return;	} 	if ( ! wp_revisions_enabled( $post ) ) {		return;	} 	/*	 * Compare the proposed update with the last stored revision verifying that	 * they are different, unless a plugin tells us to always save regardless.	 * If no previous revisions, save one.	 */	$revisions = wp_get_post_revisions( $post_id );	if ( $revisions ) {		// Grab the latest revision, but not an autosave.		foreach ( $revisions as $revision ) {			if ( str_contains( $revision->post_name, "{$revision->post_parent}-revision" ) ) {				$latest_revision = $revision;				break;			}		} 		/**		 * Filters whether the post has changed since the latest revision.		 *		 * By default a revision is saved only if one of the revisioned fields has changed.		 * This filter can override that so a revision is saved even if nothing has changed.		 *		 * @since 3.6.0		 *		 * @param bool    $check_for_changes Whether to check for changes before saving a new revision.		 *                                   Default true.		 * @param WP_Post $latest_revision   The latest revision post object.		 * @param WP_Post $post              The post object.		 */		if ( isset( $latest_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $latest_revision, $post ) ) {			$post_has_changed = false; 			foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {				if ( normalize_whitespace( $post->$field ) !== normalize_whitespace( $latest_revision->$field ) ) {					$post_has_changed = true;					break;				}			} 			/**			 * Filters whether a post has changed.			 *			 * By default a revision is saved only if one of the revisioned fields has changed.			 * This filter allows for additional checks to determine if there were changes.			 *			 * @since 4.1.0			 *			 * @param bool    $post_has_changed Whether the post has changed.			 * @param WP_Post $latest_revision  The latest revision post object.			 * @param WP_Post $post             The post object.			 */			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $latest_revision, $post );

Changelog

Introduced in 2.6.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Return type changed from int|WP_Error|void to int|WP_Error|null.verified against source
2.6.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/revision.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.