문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 wordpress.stackexchange
scroll top