0

Is there a way to check if a post written by user X is modified by a different user (for example admin, or an editor)?

What I want to achieve: I have a site where posts are submitted for publication (pending). An editor can publish the post as it is or publish the post after he does corrections. If corrections are made, I'd like to display it in some way ("This post has been published with corrections").

Even better for me would be to display the corrections too (like in a wiki history, or simply with highlights to the changed parts), but I guess a similar feature is not supported by wordpress.

1 Answer 1

0

IMO, the only draw-back of the default Post Revisions is that it stores the auto-save actions too. But, it does have a history of what changed and who changed, and seems to fullfil the OP requirements.

If some change needs to be applied to this default Meta Box, it should be removed and recreated, as it seems no hook is available. Maybe a simple jQuery is easier to remove the Autosave entries:

add_action( 'admin_head-post-new.php', 'remove_autosave_revisions_wpse_75651' );
add_action( 'admin_head-post.php', 'remove_autosave_revisions_wpse_75651' );

function remove_autosave_revisions_wpse_75651()
{
    ?>
    <script language="javascript" type="text/javascript">
    jQuery(document).ready(function($) 
    {
        $("ul.post-revisions").find("li:contains('Autosave')").remove();
    });
    </script>
    <?php
}

Case a Custom Meta Box is intended, check the following sample:

add_action( 'add_meta_boxes', 'add_custom_box_wpse_75651' );
add_action( 'save_post', 'save_postdata_wpse_75651', 10, 2 );

function add_custom_box_wpse_75651() 
{
    add_meta_box( 
        'sectionid_wpse_75651',
        __( 'Post History' ),
        'inner_custom_box_wpse_75651',
        'post' 
    );
}

function inner_custom_box_wpse_75651( $post ) 
{
    $history = get_post_meta( $post->ID, '_history', true );
    if( $history )
    {
        $reverse_history = array_reverse( $history );
        echo "
            <table class='widefat'>
                <thead>
                    <tr>
                        <th style='width:20%'>Modified by</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                    <th style='width:20%'>Modified by</th>
                    <th>Date</th>
                    </tr>
                </tfoot>
                <tbody>";

        foreach( $reverse_history as $h )
        {
            echo "
               <tr>
                  <td>
                    {$h[0]}
                  </td>
                  <td>
                    {$h[1]}
                 </td>
               </tr>            
            ";
        }

        echo "</tbody>
                    </table>
                ";
    }
}   

function save_postdata_wpse_75651( $post_id, $post_object ) 
{
    // Block auto-save
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )  
        return;

    // Block revisions
    if ( 'revision' == $post_object->post_type )
        return;

    // Check post type
    if ( 'post' == $post_object->post_type ) 
    {
        global $current_user;
        $history = get_post_meta( $post_id, '_history', true );
        $history[] = array( $current_user->user_login, current_time( 'mysql' ) );
        update_post_meta( $post_id, '_history', $history );
    }   
}

Which results in:

custom post revision meta box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.