wp_restore_post_revision

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

WordPress Version: 6.1

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|WP_Post $revision Revision ID or revision object.
 * @param array       $fields   Optional. What fields to restore from. Defaults to all.
 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision, $fields = null)
{
    $revision = wp_get_post_revision($revision, ARRAY_A);
    if (!$revision) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields($revision));
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    // Since data is from DB.
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Update last edit user.
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 5.4

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|WP_Post $revision_id Revision ID or revision object.
 * @param array       $fields      Optional. What fields to restore from. Defaults to all.
 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    $revision = wp_get_post_revision($revision_id, ARRAY_A);
    if (!$revision) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields($revision));
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    // Since data is from DB.
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Update last edit user.
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 5.3

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|WP_Post $revision_id Revision ID or revision object.
 * @param array       $fields      Optional. What fields to restore from. Defaults to all.
 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    $revision = wp_get_post_revision($revision_id, ARRAY_A);
    if (!$revision) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields($revision));
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    //since data is from db
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Update last edit user
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 4.6

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|WP_Post $revision_id Revision ID or revision object.
 * @param array       $fields      Optional. What fields to restore from. Defaults to all.
 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    if (!$revision = wp_get_post_revision($revision_id, ARRAY_A)) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields($revision));
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    //since data is from db
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Update last edit user
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 4.5

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|WP_Post $revision_id Revision ID or revision object.
 * @param array       $fields      Optional. What fields to restore from. Defaults to all.
 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    if (!$revision = wp_get_post_revision($revision_id, ARRAY_A)) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields($revision));
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    //since data is from db
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Add restore from details
    $restore_details = array('restored_revision_id' => $revision_id, 'restored_by_user' => get_current_user_id(), 'restored_time' => time());
    update_post_meta($post_id, '_post_restored_from', $restore_details);
    // Update last edit user
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 4.3

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|WP_Post $revision_id Revision ID or revision object.
 * @param array       $fields      Optional. What fields to restore from. Defaults to all.
 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    if (!$revision = wp_get_post_revision($revision_id, ARRAY_A)) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields());
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    //since data is from db
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Add restore from details
    $restore_details = array('restored_revision_id' => $revision_id, 'restored_by_user' => get_current_user_id(), 'restored_time' => time());
    update_post_meta($post_id, '_post_restored_from', $restore_details);
    // Update last edit user
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 4.1

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|object $revision_id Revision ID or revision object.
 * @param array $fields Optional. What fields to restore from. Defaults to all.
 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    if (!$revision = wp_get_post_revision($revision_id, ARRAY_A)) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields());
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    //since data is from db
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Add restore from details
    $restore_details = array('restored_revision_id' => $revision_id, 'restored_by_user' => get_current_user_id(), 'restored_time' => time());
    update_post_meta($post_id, '_post_restored_from', $restore_details);
    // Update last edit user
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 3.9

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @uses wp_get_post_revision()
 * @uses wp_update_post()
 *
 * @param int|object $revision_id Revision ID or revision object.
 * @param array $fields Optional. What fields to restore from. Defaults to all.
 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    if (!$revision = wp_get_post_revision($revision_id, ARRAY_A)) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields());
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    //since data is from db
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Add restore from details
    $restore_details = array('restored_revision_id' => $revision_id, 'restored_by_user' => get_current_user_id(), 'restored_time' => time());
    update_post_meta($post_id, '_post_restored_from', $restore_details);
    // Update last edit user
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    /**
     * Fires after a post revision has been restored.
     *
     * @since 2.6.0
     *
     * @param int $post_id     Post ID.
     * @param int $revision_id Post revision ID.
     */
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}

WordPress Version: 3.7

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @uses wp_get_post_revision()
 * @uses wp_update_post()
 * @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()
 *  is successful.
 *
 * @param int|object $revision_id Revision ID or revision object.
 * @param array $fields Optional. What fields to restore from. Defaults to all.
 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
 */
function wp_restore_post_revision($revision_id, $fields = null)
{
    if (!$revision = wp_get_post_revision($revision_id, ARRAY_A)) {
        return $revision;
    }
    if (!is_array($fields)) {
        $fields = array_keys(_wp_post_revision_fields());
    }
    $update = array();
    foreach (array_intersect(array_keys($revision), $fields) as $field) {
        $update[$field] = $revision[$field];
    }
    if (!$update) {
        return false;
    }
    $update['ID'] = $revision['post_parent'];
    $update = wp_slash($update);
    //since data is from db
    $post_id = wp_update_post($update);
    if (!$post_id || is_wp_error($post_id)) {
        return $post_id;
    }
    // Add restore from details
    $restore_details = array('restored_revision_id' => $revision_id, 'restored_by_user' => get_current_user_id(), 'restored_time' => time());
    update_post_meta($post_id, '_post_restored_from', $restore_details);
    // Update last edit user
    update_post_meta($post_id, '_edit_last', get_current_user_id());
    do_action('wp_restore_post_revision', $post_id, $revision['ID']);
    return $post_id;
}