PHPackages                             soulseekah/wp-lock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. soulseekah/wp-lock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

soulseekah/wp-lock
==================

A library for locking and mutexes for the WordPress Core

525255[1 issues](https://github.com/soulseekah/wp-lock/issues)PHPCI failing

Since Apr 18Pushed 3y ago3 watchersCompare

[ Source](https://github.com/soulseekah/wp-lock)[ Packagist](https://packagist.org/packages/soulseekah/wp-lock)[ RSS](/packages/soulseekah-wp-lock/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (3)Used By (0)

`WP_Lock`
=========

[](#wp_lock)

because WordPress is not thread-safe
------------------------------------

[](#because-wordpress-is-not-thread-safe)

[![PHPUnit Tests](https://github.com/soulseekah/wp-lock/actions/workflows/phpunit.yml/badge.svg)](https://github.com/soulseekah/wp-lock/actions/workflows/phpunit.yml)

WordPress is no longer just a blogging platform. It's a framework. And like all mature frameworks it drastically needs a lock API.

Example
-------

[](#example)

Consider the following user balance topup function that is susceptible to a race condition:

```
// topup function that is not thread-safe
public function topup_user_balance( $user_id, $topup ) {
	$balance = get_user_meta( $user_id, 'balance', true );
	$balance = $balance + $topup;
	update_user_meta( $user_id, 'balance', $balance );
	return $balance;
}
```

Try to call the above code 100 times in 16 threads. The balance will be less than it is supposed to be.

```
// A thread-safe version of the above topup function.
public function topup_user_balance( $user_id, $topup ) {
	$user_balance_lock = new WP_Lock( "$user_id:meta:balance" );
	$user_balance_lock->acquire( WP_Lock::WRITE );

	$balance = get_user_meta( $user_id, 'balance', true );
	$balance = $balance + $topup;
	update_user_meta( $user_id, 'balance', $balance );

	$user_balance_lock->release();

	return $balance;
}
```

The above code is thread safe.

Lock levels
-----------

[](#lock-levels)

- `WP_Lock::READ` - other processes can acquire READ but not WRITE until the original lock is released. A shared read lock.
- `WP_Lock::WRITE` (default) - other processes can't acquire READ or WRITE locks until the original lock is released. An exclusive read-write lock

Usage
-----

[](#usage)

Require via Composer `composer require soulseekah/wp-lock` in your plugin.

```
use soulseekah\WP_Lock;

require 'vendor/autoload.php';

$lock = new WP_Lock\WP_Lock( 'my-first-lock' );
```

Caveats
-------

[](#caveats)

In highly concurrent setups you may get Deadlock errors from MySQL. This is normal. The library handles these gracefully and retries the query as needed.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 Bus Factor1

Top contributor holds 94.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/113c5f9278504b2e6d55fce1f8412914ed6328ff34c3b9e099493e36d2f875b7?d=identicon)[soulseekah](/maintainers/soulseekah)

---

Top Contributors

[![soulseekah](https://avatars.githubusercontent.com/u/685880?v=4)](https://github.com/soulseekah "soulseekah (50 commits)")[![andriibieriezhnoi](https://avatars.githubusercontent.com/u/13201155?v=4)](https://github.com/andriibieriezhnoi "andriibieriezhnoi (1 commits)")[![verne-wv](https://avatars.githubusercontent.com/u/25538462?v=4)](https://github.com/verne-wv "verne-wv (1 commits)")[![versusbassz](https://avatars.githubusercontent.com/u/2342074?v=4)](https://github.com/versusbassz "versusbassz (1 commits)")

---

Tags

concurrencywordpress

### Embed Badge

![Health badge](/badges/soulseekah-wp-lock/health.svg)

```
[![Health](https://phpackages.com/badges/soulseekah-wp-lock/health.svg)](https://phpackages.com/packages/soulseekah-wp-lock)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
