WordPress文章锁定机制:如何判定用户停止编辑及推断状态?
Great question—you’ve already nailed the two core pieces of the puzzle with the AJAX heartbeats and wp_postmeta storage. Let me connect the dots on how the system actually infers when a user has stopped editing:
The heartbeat request’s role in extending the lock
Every ~15 seconds, the WordPress editor sends an AJAX request towp-admin/admin-ajax.phpwith theheartbeataction. This request doesn’t just "refresh" the lock—it actively updates the_edit_lockmeta value in thewp_postmetatable. The value stored is a string formatted like{user_id}:{expiration_timestamp}, where the expiration timestamp is set to the current server time plus 15 seconds (the interval of the next expected heartbeat).Detecting inactivity via expired timestamps
WordPress doesn’t need to "watch" for a user to explicitly stop editing. Instead, it uses a passive expiration check:- If the user keeps editing (and the heartbeats keep firing), the
_edit_locktimestamp gets updated every 15 seconds, pushing the expiration further out. - When the user stops editing—whether they close the tab, navigate away, lose internet, or just step away—the heartbeat requests stop. The
_edit_locktimestamp stops being updated, and eventually, the current server time will pass that expiration value.
- If the user keeps editing (and the heartbeats keep firing), the
Checking lock status for other users
When another admin tries to edit the same post, WordPress checks the_edit_lockmeta value:- If the expiration timestamp is still in the future, it shows the post as locked by the original user.
- If the timestamp is in the past, it treats the lock as expired, allowing the new user to take over editing (and start their own heartbeat cycle to update the lock).
And you’re right—this system doesn’t use WebSockets at all. The periodic AJAX heartbeats are lightweight enough for this low-frequency state sync, and the expiration-based check keeps the logic simple and reliable.
内容的提问来源于stack exchange,提问作者i.brod

