wp_autosave( array $post_data ): mixed
- Since
- 3.9.0
- Source
wp-admin/includes/post.php:2140
Description
Intended for use with heartbeat and autosave.js
Compatibility
- WordPress
- since 3.9.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- Associative array of the submitted post data.
Return value
mixed- The value 0 or WP_Error on failure. The saved post ID on success.
The ID can be the draft post_id or the autosave revision post_id.
Performance profile
How much work a call to wp_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
- Constant
- Instructions
- 26–72
- Plugin surface
- None
- Called by
- 1
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 95.
Nothing here hands control to plugin code.
1 place 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()one call below wp_autosave()
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 · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_autosave() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 26–27 | wp_verify_nonce(), __() |
!current_user_can() | 37–38 | wp_verify_nonce(), get_post(), current_user_can(), __() |
current_user_can() && wp_check_post_lock() | 48–53 | wp_verify_nonce(), get_post(), current_user_can(), wp_check_post_lock(), wp_slash(), wp_create_post_autosave() |
current_user_can() && !wp_check_post_lock() | 54–65 | wp_verify_nonce(), get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_slash(), wp_create_post_autosave() |
current_user_can() && !wp_check_post_lock() | 57–65 | wp_verify_nonce(), get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_slash(), edit_post() |
current_user_can() && !empty($post_data) && wp_check_post_lock() | 57–60 | wp_verify_nonce(), get_post(), current_user_can(), explode(), wp_check_post_lock(), wp_slash(), wp_create_post_autosave() |
current_user_can() && !empty($post_data) && !wp_check_post_lock() | 63–72 | wp_verify_nonce(), get_post(), current_user_can(), explode(), wp_check_post_lock(), get_current_user_id(), wp_slash(), wp_create_post_autosave() |
current_user_can() && !empty($post_data) && !wp_check_post_lock() | 66–72 | wp_verify_nonce(), get_post(), current_user_can(), explode(), wp_check_post_lock(), get_current_user_id(), wp_slash(), edit_post() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 95 instructions, 26–72 executed per call, 10 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.
Uses · 10
- wp_verify_nonce()Verifies that a correct security nonce was used with time limit.
- __()Retrieves the translation of $text.
- get_post()Retrieves post data given a post ID or post object.
- current_user_can()Returns whether the current user has the specified capability.
- wp_check_post_lock()Determines whether the post is currently being edited by another user.
- get_current_user_id()Gets the current user's ID.
- edit_post()Updates an existing post with values provided in `$_POST`.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_create_post_autosave()Creates autosave data for the specified post from `$_POST` data.
- WP_Error::__construct()Initializes the error.
Used by · 1
- heartbeat_autosave()Performs autosave with heartbeat.
Source code
function wp_autosave( $post_data ) { // Back-compat. if ( ! defined( 'DOING_AUTOSAVE' ) ) { define( 'DOING_AUTOSAVE', true ); } $post_id = (int) $post_data['post_id']; $post_data['ID'] = $post_id; $post_data['post_ID'] = $post_id; if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) { return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) ); } $post = get_post( $post_id ); if ( ! current_user_can( 'edit_post', $post->ID ) ) { return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) ); } if ( 'auto-draft' === $post->post_status ) { $post_data['post_status'] = 'draft'; } if ( 'page' !== $post_data['post_type'] && ! empty( $post_data['catslist'] ) ) { $post_data['post_category'] = explode( ',', $post_data['catslist'] ); } if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() === (int) $post->post_author && ( 'auto-draft' === $post->post_status || 'draft' === $post->post_status ) ) { // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked. return edit_post( wp_slash( $post_data ) ); } else { /* * Non-drafts or other users' drafts are not overwritten. * The autosave is stored in a special post revision for each user. */ return wp_create_post_autosave( wp_slash( $post_data ) ); }}Changelog
Introduced in 3.9.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.8.8 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.