Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chandrapatel/1696470ed345b339b7cfec587209669c to your computer and use it in GitHub Desktop.
Save chandrapatel/1696470ed345b339b7cfec587209669c to your computer and use it in GitHub Desktop.
Post Meta Revisions
<?php
/*
Plugin Name: Post Meta Revisions
Description: Revisions for the 'foo' post meta field
Version: 1.0
Author: John Blackbourn
Plugin URI: http://lud.icro.us/post-meta-revisions-wordpress
*/
function pmr_fields( $fields ) {
$fields['foo'] = 'Foo';
return $fields;
}
function pmr_field( $value, $field ) {
global $revision;
return get_metadata( 'post', $revision->ID, $field, true );
}
function pmr_restore_revision( $post_id, $revision_id ) {
$post = get_post( $post_id );
$revision = get_post( $revision_id );
$meta = get_metadata( 'post', $revision->ID, 'foo', true );
if ( false === $meta )
delete_post_meta( $post_id, 'foo' );
else
update_post_meta( $post_id, 'foo', $meta );
}
function pmr_save_post( $post_id, $post ) {
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$meta = get_post_meta( $parent_id, 'foo', true );
if ( false !== $meta )
add_metadata( 'post', $post_id, 'foo', $meta );
}
}
add_filter( '_wp_post_revision_field_foo', 'pmr_field', 10, 2 );
add_action( 'save_post', 'pmr_save_post', 10, 2 );
add_action( 'wp_restore_post_revision', 'pmr_restore_revision', 10, 2 );
add_filter( '_wp_post_revision_fields', 'pmr_fields' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment