On this page
wp_create_post_autosave( array|int $post_data ): int|WP_Error
Creates autosave data for the specified post from $_POST data.
Parameters
$post_dataarray|int Required-
Associative array containing the post data, or integer post ID.
If a numeric post ID is provided, will use the$_POSTsuperglobal.
Return
int|WP_Error The autosave revision ID. WP_Error or 0 on error.
Source
File: wp-admin/includes/post.php. View all references
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
*
* @param array $new_autosave Post array - the autosave that is about to be saved.
*/
do_action( 'wp_creating_autosave', $new_autosave );
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.
return _wp_put_post_revision( $post_data, true );
}
Hooks
- do_action( 'wp_creating_autosave',
array $new_autosave ) -
Fires before an autosave is stored.
Related
Uses
| Uses | Description |
|---|---|
| _wp_get_allowed_postdata() wp-admin/includes/post.php | Returns only allowed post data fields. |
| _wp_post_revision_data() wp-includes/revision.php | Returns a post array ready to be inserted into the posts table as a post revision. |
| _wp_translate_postdata() wp-admin/includes/post.php | Renames |
| normalize_whitespace() wp-includes/formatting.php | Normalizes EOL characters and strips duplicate whitespace. |
| wp_update_post() wp-includes/post.php | Updates a post with new post data. |
| wp_delete_post_revision() wp-includes/revision.php | Deletes a revision. |
| _wp_put_post_revision() wp-includes/revision.php | Inserts post data into the posts table as a post revision. |
| wp_get_post_autosave() wp-includes/revision.php | Retrieves the autosaved data of the specified post. |
| _wp_post_revision_fields() wp-includes/revision.php | Determines which fields of posts are to be saved in revisions. |
| wp_unslash() wp-includes/formatting.php | Removes slashes from a string or recursively removes slashes from strings within an array. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| get_current_user_id() wp-includes/user.php | Gets the current user’s ID. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| WP_Customize_Manager::save_changeset_post() wp-includes/class-wp-customize-manager.php | Saves the post for the loaded changeset. |
| post_preview() wp-admin/includes/post.php | Saves a draft or manually autosaves for the purpose of showing a post preview. |
| wp_autosave() wp-admin/includes/post.php | Saves a post submitted with XHR. |
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_create_post_autosave