wp_create_post_autosave( array|int $post_data ): int|WP_Error
- Since
- 2.6.0
- Source
wp-admin/includes/post.php:1957
$_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$_POSTsuperglobal.
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
- Scaling
- Scales with input
- Instructions
- 15–83
- Plugin surface
- 1 hook
- 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 118.
Third-party callbacks on 'wp_creating_autosave' 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
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.
| When | Instructions | Calls 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 !== 0 | 53–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 118 | 15–83 | 9 | |
| 8.5 | 118 | 15–83 | 9 | |
| 8.4 | 118 | 15–83 | 9 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 120 | 17–85 | 9 | |
| 8.2 | 120 | 17–85 | 9 | |
| 8.1 | 120 | 17–85 | 9 | |
| 7.4 | 120 | 17–85 | 9 |
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:
- do_action( wp_creating_autosave )actionline 2005 (+48 into the body)
Fires before an autosave is stored.
- do_action( wp_creating_autosave )actionline 2018 (+61 into the body)
Fires before an autosave is stored.
Uses · 14
- _wp_translate_postdata()Renames `$_POST` data from form names to DB post columns.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _wp_get_allowed_postdata()Returns only allowed post data fields.
- get_current_user_id()Gets the current user's ID.
- wp_get_post_autosave()Retrieves the autosaved data of the specified post.
- _wp_post_revision_data()Returns a post array ready to be inserted into the posts table as a post revision.
- get_post()Retrieves post data given a post ID or post object.
- _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_delete_post_revision()Deletes a revision.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_update_post()Updates a post with new post data.
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
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- post_preview()Saves a draft or manually autosaves for the purpose of showing a post preview.
- wp_autosave()Saves a post submitted with XHR.
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.
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.