How can I keep the content of my pages version controlled?

You already got something like this built in: Revisions.

// Define the nr of saved revisions in your wp-config.php
define( 'WP_POST_REVISIONS', 30 );

You can simply grab them by calling get_posts() with a post_type of revision.

To show the difference between two revisions simply use wp_text_diff().

// Example
$revisions = get_posts( array(
    'post_type' => 'revision'
) );
echo wp_text_diff(
     $revisions[0]['post_content']
    ,$revisions[1]['post_content']
    ,array(
         'title'       => 'Revision diff'
        ,'title_left'  => $revisions[0]['post_title']
        ,'title_right' => $revisions[1]['post_title']
     )
);

To diff for e.g. the last version with the version prior to the last, you could use end( $revisions )['post_content'] and diff it with $revisions[ count( $revisions ) -2 ]['post_content']. (Note: -2 as the arrays index start with zero and you want the version prior to the last.).

Leave a Comment