0

I use CMB2 to add some custom fields to my posts.

If i change the values of those custom fields and update the post, it doesn't create a new revision. a new revision is created only when i change the core content of the post (title, content, thumbnail...).

Is it possible to make Wordpress create a new revision even when i change only the custom fields?

  • Thanks, i will take a look! weird i didn't found those long searching... – Ben Nov 28 '16 at 19:43
  • actually now i see, the first one talks about saving the custom fields to a new revision, after a new revision is created, but doesn't address the issue of making Wordpress actually create a new revision when only the custom fields are being changed (my question). the second one is also not really relevant to this question. but thanks anyway! – Ben Nov 28 '16 at 19:47
  • we get to a question what is a post revision. Can a post revision hold the same post content? It can if you define the actions below. But you are correct, this may not be the duplicate question. – prosti Nov 28 '16 at 20:17
0

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.