public
Last active

Post Meta Revisions

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
<?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' );
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.