_wp_put_post_revision( int|WP_Post|array|null $post = null, bool $autosave = false ): int|WP_Error
- Since
- 2.6.0
- Source
wp-includes/revision.php:354
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
$postint|WP_Post|array|nulloptional- Post ID, post object OR post array.Default:
null $autosavebooloptional- Whether the revision is an autosave or not.
Default false.Default:false
Return value
int|WP_Error- WP_Error or 0 if error, new revision ID if success.
Performance profile
How much work a call to _wp_put_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
- Constant
- Instructions
- 15–47
- Plugin surface
- 1 hook
- Called by
- 3
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 69.
Third-party callbacks on '_wp_put_post_revision' 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
do_action()called directly - optionoption read or write
get_option()one call below _wp_put_post_revision()
Further down the call graph this can also reach 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 · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_put_post_revision() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_object($post) && is_array($post) | 15–22 | __() |
is_object($post) | 18–25 | get_object_vars(), __() |
!is_object($post) && !is_array($post) | 21–28 | get_post(), __() |
!is_object($post) && is_array($post) && !empty($post) | 30–34 | _wp_post_revision_data(), wp_slash(), wp_insert_post(), is_wp_error() |
is_object($post) && !empty($post) | 33–37 | get_object_vars(), _wp_post_revision_data(), wp_slash(), wp_insert_post(), is_wp_error() |
!is_object($post) && !is_array($post) && !empty($post) | 36–40 | get_post(), _wp_post_revision_data(), wp_slash(), wp_insert_post(), is_wp_error() |
!is_object($post) && is_array($post) && !empty($post) && !is_wp_error() | 38–41 | _wp_post_revision_data(), wp_slash(), wp_insert_post(), is_wp_error(), do_action() |
is_object($post) && !empty($post) && !is_wp_error() | 41–44 | get_object_vars(), _wp_post_revision_data(), wp_slash(), wp_insert_post(), is_wp_error(), do_action() |
!is_object($post) && !is_array($post) && !empty($post) && !is_wp_error() | 44–47 | get_post(), _wp_post_revision_data(), wp_slash(), wp_insert_post(), is_wp_error(), do_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 69 instructions, 15–47 executed per call, 8 branches. The work does not change between versions.
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 · 1
One hook fires while _wp_put_post_revision() runs, in this order:
- do_action( _wp_put_post_revision )actionline 387 (+33 into the body)
Fires once a revision has been saved.
Uses · 8
- get_post()Retrieves post data given a post ID or post object.
- __()Retrieves the translation of $text.
- _wp_post_revision_data()Returns a post array ready to be inserted into the posts table as a post revision.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_insert_post()Inserts or update a post.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Error::__construct()Initializes the error.
Used by · 3
- WP_REST_Autosaves_Controller::create_post_autosave()Creates autosave for the specified post.
- wp_create_post_autosave()Creates autosave data for the specified post from `$_POST` data.
- wp_save_post_revision()Creates a revision for the current version of a post.
Source code
function _wp_put_post_revision( $post = null, $autosave = false ) { if ( is_object( $post ) ) { $post = get_object_vars( $post ); } elseif ( ! is_array( $post ) ) { $post = get_post( $post, ARRAY_A ); } if ( ! $post || empty( $post['ID'] ) ) { return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); } if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) { return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); } $post = _wp_post_revision_data( $post, $autosave ); $post = wp_slash( $post ); // Since data is from DB. $revision_id = wp_insert_post( $post, true ); if ( is_wp_error( $revision_id ) ) { return $revision_id; } if ( $revision_id ) { /** * Fires once a revision has been saved. * * @since 2.6.0 * @since 6.4.0 The post_id parameter was added. * * @param int $revision_id Post revision ID. * @param int $post_id Post ID. */ do_action( '_wp_put_post_revision', $revision_id, $post['post_parent'] ); } return $revision_id;}Changelog
Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About 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.