wppaste
WordPress

_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
Inserts post data into the posts table as a post revision.

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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–47

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

Plugin surface
1 hook

Third-party callbacks on '_wp_put_post_revision' 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 callbacksdo_action()called directly
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
!is_object($post) && is_array($post)15–22__()
is_object($post)18–25get_object_vars(), __()
!is_object($post) && !is_array($post)21–28get_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–37get_object_vars(), _wp_post_revision_data(), wp_slash(), wp_insert_post(), is_wp_error()
!is_object($post) && !is_array($post) && !empty($post)36–40get_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–44get_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–47get_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:

  1. do_action( _wp_put_post_revision )actionline 387 (+33 into the body)

    Fires once a revision has been saved.

Uses · 8

Used by · 3

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.

  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.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.