I can use wp_set_post_lock to temporarily lock a post so that only I can edit it.

I can use wp_refresh_post_lock to refresh that lock.

I can use wp_check_post_lock to check whether a post is locked.

...but how can I remove the post lock? My assumption would be there's a function called wp_remove_post_lock or wp_release_post_lock, but neither of those seem to exist.

So how can I remove the post lock? Surely I don't have to wait for it to time out, right?

up vote 2 down vote accepted

At a first glance it makes sense, but...

When exactly would that function be used? When user opens post editor, you can easily hook to that action and set the lock.

But when would you remove it? After saving? No - user is still editing, so the lock should be on.

It should be removed after user has closed the tab or closed the editor - but you can’t hook to these actions from PHP, because there PHP doesn’t get notified about them just before they happen...

So most probably there is no function for removing lock, because there is no use for it in normal usage...

Of course you can still easily remove such lock...

Let’s look what exactly is that lock and how WP sets it:

function wp_set_post_lock( $post_id ) {
    if ( ! $post = get_post( $post_id ) ) {
        return false;
    }
 
    if ( 0 == ( $user_id = get_current_user_id() ) ) {
        return false;
    }
 
    $now = time();
    $lock = "$now:$user_id";
 
    update_post_meta( $post->ID, '_edit_lock', $lock );
 
    return array( $now, $user_id );
}

OK, so it’s stored as custom filed called '_edit_lock', so... Just remove this meta and the lock will be removed.

delete_post_meta( $post_id, '_edit_lock')
  • 1
    ps: there exists an ajax handler wp_ajax_wp_remove_post_lock() – birgire Dec 1 at 9:19
  • I ended up using update_post_meta( $post_id, '_edit_lock', false) rather than deleting it, but your solution should work as well. – Pikamander2 Dec 2 at 6:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.