wp_check_post_lock() WordPress Function

The wp_check_post_lock() function is used to check if a post is currently locked by another user. If a post is locked, the current user will not be able to edit it.

wp_check_post_lock( int|WP_Post $post_id ) #

Determines whether the post is currently being edited by another user.


Parameters

$post_id

(int|WP_Post)(Required)ID or object of the post to check for editing.


Top ↑

Return

(int|false) ID of the user with lock. False if the post does not exist, post is not locked, the user with lock does not exist, or the post is locked by current user.


Top ↑

Source

File: wp-admin/includes/post.php

function wp_check_post_lock( $post_id ) {
	$post = get_post( $post_id );
	if ( ! $post ) {
		return false;
	}

	$lock = get_post_meta( $post->ID, '_edit_lock', true );
	if ( ! $lock ) {
		return false;
	}

	$lock = explode( ':', $lock );
	$time = $lock[0];
	$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );

	if ( ! get_userdata( $user ) ) {
		return false;
	}

	/** This filter is documented in wp-admin/includes/ajax-actions.php */
	$time_window = apply_filters( 'wp_check_post_lock_window', 150 );

	if ( $time && $time > time() - $time_window && get_current_user_id() != $user ) {
		return $user;
	}

	return false;
}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More