Still, I found it is good to update some more details on the excellent post.
Save your custom fields:
add_action( '_wp_put_post_revision', 'save_cmb2' );
function save_cmb2( $post, true ) {
// save the custom field
}
Note that the second parameter is true
if you like autosave.
File: wp-includes/revision.php
275: /**
276: * Inserts post data into the posts table as a post revision.
277: *
278: * @since 2.6.0
279: * @access private
280: *
281: * @param int|WP_Post|array|null $post Post ID, post object OR post array.
282: * @param bool $autosave Optional. Is the revision an autosave?
283: * @return int|WP_Error WP_Error or 0 if error, new revision ID if success.
284: */
285: function _wp_put_post_revision( $post = null, $autosave = false ) {
You can restore a revision if you use wp_restore_post_revision
function.
/**
* 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 ) {
…
There is a hook defined inside that function you should use.:
File: wp-includes/revision.php
392: /**
393: * Fires after a post revision has been restored.
394: *
395: * @since 2.6.0
396: *
397: * @param int $post_id Post ID.
398: * @param int $revision_id Post revision ID.
399: */
400: do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
This will lead you to the code like this:
add_action( 'wp_restore_post_revision', 'restore_cmb2' );
function restore_cmb2( $post ) {
// restore the custom field
}