Restrict access to post if it is currently being edited

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