0

I need to show history of post changes (post revisions) at the end of post content.

I saw tips about "Last modified by/date" feature but I need to show the table of all changes (revisions) done on post (Date/author/content- if possible).

It's formally required for public institutions and I'm bit surprised there are nothing on Google. Is there a way to do that?

2

There are many ways to do it, without third party plugins:

Shortcode

Cons: shortcode has to be created, and added to all posts.

Child theme template

Cons: theme dependent, and right template has to be modified.

Child theme functions.php

Cons: theme dependent.

Plugin in mu-plugins

Cons: none. Pros: not theme dependent, easiest to implement.

Implementation

Create file post-with-revisions.php, with the following content, and place it in wp-content/mu-plugins:

<?php
function wpse_single_post_with_revisions( $content ) {
 
    // Check if we're inside the main loop in a single Post.
    if ( is_singular() && in_the_loop() && is_main_query() ) {
        $content .= '<h2>Revisions</h2>';
        $revisions = wp_get_post_revisions();
        foreach ( $revisions as $rev ) {
            $date = $rev->post_date;
            $author = get_author_name( $auth_id = $rev->post_author );
            $content .= '<h4>' . $date . ' by ' . $author . '</h4>';
            $content .= $rev->post_content;
        }            
    }
    return $content;
}
add_filter( 'the_content', 'wpse_single_post_with_revisions' );

Note: revisions will be visible in single post only, not in archives.

UPDATE

For easier identification of changes between revisions, instead of displaying the content of revisions, we can display differences between them. The modified code follows:

<?php
function wpse_single_post_with_revisions( $content ) {
    global $post;

    // Check if we're inside the main loop in a single Post.
    if ( is_singular() && in_the_loop() && is_main_query() ) {
        $content .= '<h2>Revisions</h2>';
        $revisions = wp_get_post_revisions();
        $ids_to_compare = array();
        foreach ( $revisions as $rev ) {
            $date = $rev->post_date;
            $author = get_author_name( $auth_id = $rev->post_author );
            $id = $rev->ID;
            array_push( $ids_to_compare, (int) $id );
            $content .= '<strong>ID: ' . $id .' - ' . $date . ' by ' . $author . '</strong><br>';
            //$content .= $rev->post_content;
        }
        $content .= '<h2>Diffs</h2>';

        require 'wp-admin/includes/revision.php';

        for ( $i = 0; $i <= count( $ids_to_compare ) - 2; $i++ ) {
            $diffs =  wp_get_revision_ui_diff( $post, $ids_to_compare[$i], $ids_to_compare[$i + 1] );
            $content .= '<h3>' . $ids_to_compare[$i] . ' to ' . $ids_to_compare[$i + 1] . '</h3>';
            if ( is_array( $diffs ) ) {
                foreach ( $diffs as $diff ) {
                    $content .= $diff['diff'];
                }
            }
            $content .= '<hr>';
        }
    }
    return $content;
}
add_filter( 'the_content', 'wpse_single_post_with_revisions' );
3
  • 1
    As long as you're only tracking content, this will work great. If you also need to track changes to postmeta, you'll also have to build in postmeta revisions as they do not natively exist. – WebElaine Apr 16 at 15:56
  • Nice point, My answer provides just a starting point, and the code can be further modified as necessary. – Frank P. Walentynowicz Apr 16 at 16:07
  • Thank you Frank. It looks like something I was looking for. – Mateusz Graczyk Apr 17 at 12:04
0

WordPress does not natively do that. You may want to try plugins such as Simple History.

Check this out https://publishpress.com/blog/plugins-track-content-changes-wordpress/

2
  • Third party plugins recommendation does not have real educational value. Actual code can be useful to everybody. – Frank P. Walentynowicz Apr 16 at 15:57
  • @FrankP.Walentynowicz Yes, I appreciate that. Thanks for posting the code. – Dave White Apr 16 at 17:13

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.