wp_save_post_revision( int $post_id ): int|WP_Error|void
- Since
- 2.6.0
- Source
wp-includes/revision.php:130
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
- Scaling
- Scales with input
- Instructions
- 6–124
- Plugin surface
- 3 hooks
- Called by
- 3
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 144.
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.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
defined('DOING_AUTOSAVE') | 6 | none |
!doing_action() | 13–15 | doing_action(), get_post() |
doing_action() && has_action() | 13–15 | doing_action(), has_action() |
doing_action() && !has_action() | 18–20 | doing_action(), has_action(), get_post() |
!doing_action() | 20–25 | doing_action(), get_post(), post_type_supports() |
doing_action() && !has_action() | 25–30 | doing_action(), has_action(), get_post(), post_type_supports() |
!doing_action() && post_type_supports() && !wp_revisions_enabled() | 27–29 | doing_action(), get_post(), post_type_supports(), wp_revisions_enabled() |
doing_action() && !has_action() && post_type_supports() && !wp_revisions_enabled() | 32–34 | doing_action(), has_action(), get_post(), post_type_supports(), wp_revisions_enabled() |
!doing_action() && post_type_supports() && wp_revisions_enabled() && $revisions_to_keep | 42–55 | doing_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_keep | 47–60 | doing_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_keep | 53–62 | doing_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 && $delete | 57–70 | doing_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_keep | 58–67 | doing_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–71 | doing_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 && $delete | 62–75 | doing_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–76 | doing_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–80 | doing_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 && $delete | 68–77 | doing_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_keep | 71–81 | doing_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–85 | doing_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 && $delete | 73–82 | doing_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–84 | doing_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 | 76–86 | doing_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–87 | doing_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–89 | doing_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–92 | doing_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_keep | 85–94 | doing_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 && $delete | 86–96 | doing_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_keep | 90–99 | doing_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 && $delete | 91–101 | doing_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–106 | doing_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 && $delete | 100–109 | doing_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–111 | doing_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 && $delete | 105–114 | doing_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–119 | doing_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–124 | doing_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 144 | 6–124 | 22 | |
| 8.5 | 144 | 6–124 | 22 | |
| 8.4 | 144 | 6–124 | 22 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 150 | 6–127 | 22 | |
| 8.2 | 150 | 6–127 | 22 | |
| 8.1 | 150 | 6–127 | 22 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 152 | 6–128 | 22 |
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:
- 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.
- apply_filters( wp_save_post_revision_post_has_changed )filterline 208 (+78 into the body)
Filters whether a post has changed.
- 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
- doing_action()Returns whether or not an action hook is currently being processed.
- has_action()Checks if any action has been registered for a hook.
- get_post()Retrieves post data given a post ID or post object.
- post_type_supports()Checks a post type's support for a given feature.
- wp_revisions_enabled()Determines whether revisions are enabled for a given post.
- wp_get_post_revisions()Returns all revisions of specified post.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _wp_post_revision_fields()Determines which fields of posts are to be saved in revisions.
- normalize_whitespace()Normalizes EOL characters and strips duplicate whitespace.
- _wp_put_post_revision()Inserts post data into the posts table as a post revision.
- wp_revisions_to_keep()Determines how many revisions to retain for a given post.
Show all 13
- wp_delete_post_revision()Deletes a revision.
Used by · 3
- _wp_upgrade_revisions_of_post()Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1.
- wp_save_post_revision_on_insert()Saves revisions for a post after all changes have been made.
- wp_update_custom_css_post()Updates the `custom_css` post for a given theme.
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.
Signature, return type and hooks compared across 5 parsed releases.
int|WP_Error|void to int|WP_Error|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.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.