wp_autosave

The timeline below displays how wordpress function wp_autosave has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Saves a post submitted with XHR.
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return 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.
 */
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() == $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));
    }
}

WordPress Version: 5.9

/**
 * Saves a post submitted with XHR.
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return 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.
 */
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() == $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));
    }
}

WordPress Version: 5.5

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return 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.
 */
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() == $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));
    }
}

WordPress Version: 5.4

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return 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.
 */
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() == $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));
    }
}

WordPress Version: 5.3

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return 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.
 */
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 ($post_data['post_type'] != 'page' && !empty($post_data['catslist'])) {
        $post_data['post_category'] = explode(',', $post_data['catslist']);
    }
    if (!wp_check_post_lock($post->ID) && get_current_user_id() == $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));
    }
}

WordPress Version: 4.6

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return 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.
 */
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_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 ($post_data['post_type'] != 'page' && !empty($post_data['catslist'])) {
        $post_data['post_category'] = explode(',', $post_data['catslist']);
    }
    if (!wp_check_post_lock($post->ID) && get_current_user_id() == $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));
    }
}

WordPress Version: 4.5

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return 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.
 */
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_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', __('You are not allowed to edit this item.'));
    }
    if ('auto-draft' == $post->post_status) {
        $post_data['post_status'] = 'draft';
    }
    if ($post_data['post_type'] != 'page' && !empty($post_data['catslist'])) {
        $post_data['post_category'] = explode(',', $post_data['catslist']);
    }
    if (!wp_check_post_lock($post->ID) && get_current_user_id() == $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));
    }
}

WordPress Version: 4.1

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param array $post_data Associative array of the submitted post data.
 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
 *               Te ID can be the draft post_id or the autosave revision post_id.
 */
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_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', __('You are not allowed to edit this item.'));
    }
    if ('auto-draft' == $post->post_status) {
        $post_data['post_status'] = 'draft';
    }
    if ($post_data['post_type'] != 'page' && !empty($post_data['catslist'])) {
        $post_data['post_category'] = explode(',', $post_data['catslist']);
    }
    if (!wp_check_post_lock($post->ID) && get_current_user_id() == $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));
    }
}

WordPress Version: 4.0

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9.0
 *
 * @param $post_data Associative array of the submitted post data.
 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
 *               Te ID can be the draft post_id or the autosave revision post_id.
 */
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_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', __('You are not allowed to edit this item.'));
    }
    if ('auto-draft' == $post->post_status) {
        $post_data['post_status'] = 'draft';
    }
    if ($post_data['post_type'] != 'page' && !empty($post_data['catslist'])) {
        $post_data['post_category'] = explode(',', $post_data['catslist']);
    }
    if (!wp_check_post_lock($post->ID) && get_current_user_id() == $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));
    }
}

WordPress Version: 3.9

/**
 * Save a post submitted with XHR
 *
 * Intended for use with heartbeat and autosave.js
 *
 * @since 3.9
 *
 * @param $post_data Associative array of the submitted post data.
 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
 *               Te ID can be the draft post_id or the autosave revision post_id.
 */
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_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', __('You are not allowed to edit this item.'));
    }
    if ('auto-draft' == $post->post_status) {
        $post_data['post_status'] = 'draft';
    }
    if ($post_data['post_type'] != 'page' && !empty($post_data['catslist'])) {
        $post_data['post_category'] = explode(',', $post_data['catslist']);
    }
    if (!wp_check_post_lock($post->ID) && get_current_user_id() == $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));
    }
}