• The following code gets me the latest post revision upon updating a post:

    function insert_post_hook($post_id, $post) {
        var_dump(array_shift(wp_get_post_revisions($post_id)));
        exit;
    }

    add_action( ‘wp_insert_post’, ‘insert_post_hook’, 10, 2 );
    However, this only gives me som basic data like title, time, revision id etc. What I would really like, is to get the differences that has been made in this revision compared to the last one before that – like what is shown in the revisions tab of the editor.

    How would I get these specific changes?

    I tried the following approach, but that just gives me a server error. 4967 is the post id, and 4971 & 4972 are revisions of the post.

    var_dump(wp_get_revision_ui_diff(4967, 4971, 4972));

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    The server error was probably “Call to undefined function wp_get_revision_ui_diff()”. For whatever reason, the function declaration is not loaded by default, you need to explicitly load it before you can use wp_get_revision_ui_diff(). Like so:
    require_once( get_home_path() . 'wp-admin/includes/revision.php');

    Thread Starter Rallendk

    (@rallendk)

    Thank you – I got the function to work now. However, the result isn’t quite what I expected. When comparing to revisions in the editor, the result is the following:

    Screenshot

    When comparing the same revisions using wp_get_revision_ui_diff, the result is this:

    Array ( [0] => Array ( [id] => post_title [name] => Title [diff] =>
    Title #1 Title #1
    ) )

    How can I get the same revisions as the editor has?

    Moderator bcworkz

    (@bcworkz)

    Yeah, the function gives you the information you need, but you’re on your own doing something with the information. This code got me kinda close:

    require_once( get_home_path() . 'wp-admin/includes/revision.php');
    $diffs = wp_get_revision_ui_diff(665, 666, 716);
    echo '<style>
    .diff-context { width: 50%; }
    td { border-width: 0; }
    </style>';
    foreach ( $diffs as $diff ) {
       echo $diff['diff'];
    }

    You can apply more styling to get it even closer if you like.

    Thread Starter Rallendk

    (@rallendk)

    Thanks! I don’t think I explained myself clear enough tho..

    The problem is, that the revisions in editor gets me all changes, whilst the code only gives me the title.

    Those are examples for the same revisions of the same post:

    Editor:
    https://i.stack.imgur.com/hf5Dk.png

    Your code snippet:
    Title #1 Title #1

    Moderator bcworkz

    (@bcworkz)

    My code merely outputs what is returned by wp_get_revision_ui_diff(). If you are not seeing content changes, then wp_get_revision_ui_diff() did not pick up any content changes. You should double check the post IDs used. I did test the code on my installation and it returned changes to content as expected.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get differences in post revisions’ is closed to new replies.