I have am looking at the way to prevent multiple users opening same post and editing at the same time. For example I open it in one tab and start editing, when someone else wants to open in another tab it shouldnt be possible to edit it.Only one person at the time. With Wordpress, 2 users cannot edit the same post at the same time. Does anyone know WordPress how does it ? I don't know myself.
I am looking for the solution , thats why i dont know what to try exactly.
CodePudding user response:
If you want to know whether a post (page, product, any custom post type) is currently being edited, use wp_check_post_lock( $post_id ). If a user is currently editing the post, it returns the user's ID. Otherwise it returns false, and you can proceed to edit it.
If you want to mark a post as being edited, use wp_set_post_lock( $post_id ). Calling this will silently override any existing lock, so check first. You should call this function every two minutes, or more often, while editing is in progress, because locks expire after 150 seconds.
This is all implemented via a wp_postmeta entry with meta_key '_edit_lock'
and meta_value 'timestamp:userid'
. The timestamp is the time the lock was set. For example, '1667470754:123'
means userid 123 locked the post at time Thu Nov 03 2022 10:19:14Z. But avoid hitting the wp_postmeta table directly for this. The value may be cached.
You can use the check_post_lock_window filter to alter the lock expiration time if need be.
The _admin_notice_post_locked() function puts up a notice about a post being locked. But this function is designed for use within WordPress core admin pages, so it may not work for you.