Restrict access to post if it is currently being edited

On a platform with many Editors, the problem that I am experiencing is that two editors proofread the same post simultaneously (and disregarding the warning notice about someone else currently editing the post).

Is there a solution where I can restrict access to a post if it is currently being edited by a different user? Maybe by disabling the post’s edit link on the post list? This should only take effect on editors if they are not the author of the post (we don’t want them to be locked out of their own post) and admins should be excluded from any restriction.

1 Answer
1

The warning notice gets dispatched by the function wp_check_post_lock. The following redirects the user back to the post listing screen if someone else is editing it.

add_action( 'load-post.php', 'redirect_locked_post_wpse_95718' );

function redirect_locked_post_wpse_95718()
{
    if( isset($_GET['post'] ) && wp_check_post_lock( $_GET['post'] ) )
    {
        global $typenow;
        $goto = ( 'post' == $typenow ) ? '' : "?post_type=$typenow";
        wp_redirect( admin_url( "edit.php$goto" ) );
        exit();
    }
}

And to indicate that a post is locked, ie, being edited by other user, a small red sign can be added to the row actions.

locked post

foreach( array( 'post', 'page' ) as $hook )
    add_filter( "{$hook}_row_actions", 'locked_post_notice_wpse_95718', 10, 2 );

function locked_post_notice_wpse_95718( $actions, $post ) 
{
    if( wp_check_post_lock( $post->ID ) )
    {
        $actions['locked'] = sprintf(
            '<span style="color:#f00;font-weight:bolder;">&#149;&#149;&#149; LOCKED %s &#149;&#149;&#149;</span>',
            strtoupper( $post->post_type )
        );
    }
    return $actions; 
}

Leave a Comment