On this page
wp_restore_post_revision( int|WP_Post $revision, array $fields = null ): int|false|null
Restores a post to the specified revision.
Description
Can restore a past revision using all fields of the post revision, or only selected fields.
Parameters
$revisionint|WP_Post Required-
Revision ID or revision object.
$fieldsarray Optional-
What fields to restore from. Defaults to all.
Default:
null
Return
int|false|null Null if error, false if no fields to restore, (int) post ID if success.
Source
File: wp-includes/revision.php. View all references
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;
}
Hooks
- do_action( 'wp_restore_post_revision',
int $post_id ,int $revision_id ) -
Fires after a post revision has been restored.
Related
Uses
| Uses | Description |
|---|---|
| wp_update_post() wp-includes/post.php | Updates a post with new post data. |
| update_post_meta() wp-includes/post.php | Updates a post meta field based on the given post ID. |
| wp_get_post_revision() wp-includes/revision.php | Gets a post revision. |
| _wp_post_revision_fields() wp-includes/revision.php | Determines which fields of posts are to be saved in revisions. |
| wp_slash() wp-includes/formatting.php | Adds slashes to a string or recursively adds slashes to 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. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| wp_xmlrpc_server::wp_restoreRevision() wp-includes/class-wp-xmlrpc-server.php | Restore a post revision |
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_restore_post_revision