In this post, you will learn how to restrict access to post if it is currently being edited in your WordPress site. 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.
1 2 3 4 5 6 7 8 9 10 11 12 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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;">••• LOCKED %s •••</span>', strtoupper( $post->post_type ) ); } return $actions; } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.