Display post changelog in post

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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' );

Method 2

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/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x