wppaste
WordPress

wp_create_post_autosave( array|int $post_data ): int|WP_Error

Since
2.6.0
Source
wp-admin/includes/post.php:1957
Creates autosave data for the specified post from $_POST data.

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_dataarray|int
Associative array containing the post data, or integer post ID.
If a numeric post ID is provided, will use the $_POST superglobal.

Return value

int|WP_Error
The autosave revision ID. WP_Error or 0 on error.

Performance profile

How much work a call to wp_create_post_autosave() 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
15–83

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

Plugin surface
1 hook

Third-party callbacks on 'wp_creating_autosave' 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

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 · 7 distinct outcomes

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

WhenInstructionsCalls it makes
is_wp_error()15–17_wp_translate_postdata(), is_wp_error()
!is_wp_error()41–45_wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), get_current_user_id(), wp_get_post_autosave(), wp_unslash(), _wp_put_post_revision(), is_wp_error()
!is_wp_error() && $revision !== 053–55_wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), get_current_user_id(), wp_get_post_autosave(), wp_unslash(), _wp_put_post_revision(), is_wp_error(), get_post(), do_action()
!is_wp_error()64–67_wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), get_current_user_id(), wp_get_post_autosave(), _wp_post_revision_data(), get_post(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), wp_delete_post_revision()
!is_wp_error()67–70_wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), get_current_user_id(), wp_get_post_autosave(), _wp_post_revision_data(), get_post(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), do_action(), wp_update_post()
!is_wp_error() && !array_intersect()78–80_wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), get_current_user_id(), wp_get_post_autosave(), _wp_post_revision_data(), get_post(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), wp_delete_post_revision()
!is_wp_error() && !array_intersect()81–83_wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), get_current_user_id(), wp_get_post_autosave(), _wp_post_revision_data(), get_post(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), do_action(), wp_update_post()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11815–839
8.511815–839
8.411815–8392 fewer instructions than PHP 8.3
8.312017–859
8.212017–859
8.112017–859
7.412017–859

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 · 2

2 hooks fire while wp_create_post_autosave() runs, in this order:

  1. do_action( wp_creating_autosave )actionline 2005 (+48 into the body)

    Fires before an autosave is stored.

  2. do_action( wp_creating_autosave )actionline 2018 (+61 into the body)

    Fires before an autosave is stored.

Uses · 14

Show all 14
  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • _wp_put_post_revision()Inserts post data into the posts table as a post revision.

Used by · 3

Source code

function wp_create_post_autosave( $post_data ) {	if ( is_numeric( $post_data ) ) {		$post_id   = $post_data;		$post_data = $_POST;	} else {		$post_id = (int) $post_data['post_ID'];	} 	$post_data = _wp_translate_postdata( true, $post_data );	if ( is_wp_error( $post_data ) ) {		return $post_data;	}	$post_data = _wp_get_allowed_postdata( $post_data ); 	$post_author = get_current_user_id(); 	// Store one autosave per author. If there is already an autosave, overwrite it.	$old_autosave = wp_get_post_autosave( $post_id, $post_author );	if ( $old_autosave ) {		$new_autosave                = _wp_post_revision_data( $post_data, true );		$new_autosave['ID']          = $old_autosave->ID;		$new_autosave['post_author'] = $post_author; 		$post = get_post( $post_id ); 		// If the new autosave has the same content as the post, delete the autosave.		$autosave_is_different = false;		foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {			if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {				$autosave_is_different = true;				break;			}		} 		if ( ! $autosave_is_different ) {			wp_delete_post_revision( $old_autosave->ID );			return 0;		} 		/**		 * Fires before an autosave is stored.		 *		 * @since 4.1.0		 * @since 6.4.0 The `$is_update` parameter was added to indicate if the autosave is being updated or was newly created.		 *		 * @param array $new_autosave Post array - the autosave that is about to be saved.		 * @param bool  $is_update    Whether this is an existing autosave.		 */		do_action( 'wp_creating_autosave', $new_autosave, true );		return wp_update_post( $new_autosave );	} 	// _wp_put_post_revision() expects unescaped.	$post_data = wp_unslash( $post_data ); 	// Otherwise create the new autosave as a special post revision.	$revision = _wp_put_post_revision( $post_data, true ); 	if ( ! is_wp_error( $revision ) && 0 !== $revision ) { 		/** This action is documented in wp-admin/includes/post.php */		do_action( 'wp_creating_autosave', get_post( $revision, ARRAY_A ), false );	} 	return $revision;}

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-admin/includes/post.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.