Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

wp-background-processing

> 编程语言
Open source

WordPress background processing class

1.1K stars0 likes0 views
WebsiteGitHub

About

WordPress background processing class

WP Background Processing

WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks. Check out the example plugin or read the accompanying article.

Inspired by TechCrunch WP Asynchronous Tasks.

Requires PHP 5.6+

Install

The recommended way to install this library in your project is by loading it through Composer:

bash
composer require deliciousbrains/wp-background-processing

It is highly recommended to prefix wrap the library class files using the Mozart package, to prevent collisions with other projects using this same library.

Usage

Async Request

Async requests are useful for pushing slow one-off tasks such as sending emails to a background process. Once the request has been dispatched it will process in the background instantly.

Extend the WP_Async_Request class:

php
class WP_Example_Request extends WP_Async_Request {

	/**
	 * @var string
	 */
	protected $prefix = 'my_plugin';

	/**
	 * @var string
	 */
	protected $action = 'example_request';

	/**
	 * Handle a dispatched request.
	 *
	 * Override this method to perform any actions required
	 * during the async request.
	 */
	protected function handle() {
		// Actions to perform.
	}

}

protected $prefix

Should be set to a unique prefix associated with your plugin, theme, or site's custom function prefix.

protected $action

Should be set to a unique name.

protected function handle()

Should contain any logic to perform during the non-blocking request. The data passed to the request will be accessible via $_POST.

Dispatching Requests

Instantiate your request:

php
$this->example_request = new WP_Example_Request();

Add data to the request if required:

php
$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );

Fire off the request:

php
$this->example_request->dispatch();

Chaining is also supported:

php
$this->example_request->data( array( 'data' => $data ) )->dispatch();

Background Process

Background processes work in a similar fashion to async requests, but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been saved and dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed, the next batch will start instantly.

Health checks run by default every 5 minutes to ensure the queue is running when queued items exist. If the queue has failed it will be restarted.

Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing. Saving a new batch of queued items and dispatching while another background processing instance is already running will result in the dispatch shortcutting out and the existing instance eventually picking up the new items and processing them when it is their turn.

Extend the WP_Background_Process class:

…

protected $prefix

Should be set to a unique prefix associated with your plugin, theme, or site's custom function prefix.

protected $action

Should be set to a unique name.

protected function task( $item )

Should contain any logic to perform on the queued item. Return false to remove the item from the queue or return $item to push it back onto the queue for further processing. If the item has been modified and is pushed back onto the queue the current state will be saved before the batch is exited.

protected function complete()

Optionally contain any logic to perform once the queue has completed.

Dispatching Processes

Instantiate your process:

php
$this->example_process = new WP_Example_Process();

Note: You must instantiate your process unconditionally. All requests should do this, even if nothing is pushed to the queue.

Push items to the queue:

php
foreach ( $items as $item ) {
    $this->example_process->push_to_queue( $item );
}

An item can be any valid PHP value, string, integer, array or object. If needed, the $item is serialized when written to the database.

Save and dispatch the queue:

php
$this->example_process->save()->dispatch();

Handling serialized objects in queue items

Queue items that contain non-scalar values are serialized when stored in the database. To avoid potential security issues during unserialize, this library provides the option to set the allowed_classes option when calling unserialize() which limits which classes can be instantiated. It's kept internally as the protected $allowed_batch_data_classes property.

To maintain backward compatibility the default value is true, meaning that any serialized object will be instantiated. Please note that this default behavior may change in a future major release.

We encourage all users of this library to take advantage of setting a strict value for $allowed_batch_data_classes. If possible, set the value to false to disallow any objects from being instantiated, or a very limited list of class names, see examples below.

Objects in the serialized string that are not allowed to be instantiated will instead get the class type __PHP_Incomplete_Class.

Overriding the default $allowed_batch_data_classes

The default behavior can be overridden by passing an array of allowed classes to the constructor:

php
$allowed_batch_data_classes = array( MyCustomItem::class, MyItemHelper::class );
$this->example_process = new WP_Example_Process( $allowed_batch_data_classes );

Or, set the value to false:

php
$this->example_process = new WP_Example_Process( false );

Another way to change the default is to override the $allowed_batch_data_classes property in your process class:

php
class WP_Example_Process extends WP_Background_Process {

	/**
	 * @var string
	 */
	protected $prefix = 'my_plugin';

	/**
	 * @var string
	 */
	protected $action = 'example_process';

	/**
	 *
	 * @var bool|array
	 */
	protected $allowed_batch_data_classes = array( MyCustomItem::class, MyItemHelper::class );
	...

Background Process Status

A background process can be queued, processing, paused, cancelled, or none of the above (not started or has completed).

Queued

To check whether a background process has queued items use is_queued().

php
if ( $this->example_process->is_queued() ) {
    // Do something because background process has queued items, e.g. add notice in admin UI.
}
Processing

To check whether a background process is currently handling a queue of items use is_processing().

php
if ( $this->example_process->is_processing() ) {
    // Do something because background process is running, e.g. add notice in admin UI.
}
Paused

You can pause a background process with pause().

php
$this->example_process->pause();

The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron and firing a "paused" action.

To check whether a background process is currently paused use is_paused().

php
if ( $this->example_process->is_paused() ) {
    // Do something because background process is paused, e.g. add notice in admin UI.
}

You can perform an action in response to background processing being paused by handling the "paused" action for the background process's identifier ($prefix + $action).

php
add_action( 'my_plugin_example_process_paused', function() {
    // Do something because background process is paused, e.g. add notice in admin UI.
});

You can resume a background process with resume().

php
$this->example_process->resume();

You can perform an action in response to background processing being resumed by handling the "resumed" action for the background process's identifier ($prefix + $action).

php
add_action( 'my_plugin_example_process_resumed', function() {
    // Do something because background process is resumed, e.g. add notice in admin UI.
});
Cancelled

You can cancel a background process with cancel().

php
$this->example_process->cancel();

The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron, deleting all batches of queued items and firing a "cancelled" action.

To check whether a background process is currently cancelled use is_cancelled().

php
if ( $this->example_process->is_cancelled() ) {
    // Do something because background process is cancelled, e.g. add notice in admin UI.
}

You can perform an action in response to background processing being cancelled by handling the "cancelled" action for the background process's identifier ($prefix + $action).

php
add_action( 'my_plugin_example_process_cancelled', function() {
    // Do something because background process is paused, e.g. add notice in admin UI.
});

The "cancelled" action fires once the queue has been cleared down and cancelled status removed. After which is_cancelled() will no longer be true as the background process is now dormant.

Active

To check whether a background process has queued items, is processing, is paused, or is cancelling, use is_active().

php
if ( $this->example_process->is_active() ) {
    // Do something because background process is active, e.g. add notice in admin UI.
}

If a background process is not active, then it either has not had anything queued yet and not started, or has finished processing all queued items.

BasicAuth

If your site is behind BasicAuth, both async requests and background processes will fail to complete. This is because WP Background Processing relies on the WordPress HTTP API, which requires you to attach your BasicAuth credentials to requests. The easiest way to do this is using the following filter:

php
function wpbp_http_request_args( $r, $url ) {
	$r['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );

	return $r;
}
add_filter( 'http_request_args', 'wpbp_http_request_args', 10, 2);

Contributing

Contributions are welcome via Pull Requests, but please do raise an issue before working on anything to discuss the change if there isn't already an issue. If there is an approved issue you'd like to tackle, please post a comment on it to let people know you're going to have a go at it so that effort isn't wasted through duplicated work.

Unit & Style Tests

When working on the library, please add unit tests to the appropriate file in the tests directory that cover your changes.

Setting Up

We use the standard WordPress test libraries for running unit tests.

Please run the following command to set up the libraries:

bash
bin/install-wp-tests.sh db_name db_user db_pass

Substitute db_name, db_user and db_pass as appropriate.

Please be aware that running the unit tests is a destructive operation, database tables will be cleared, so please use a database name dedicated to running unit tests. The standard database name usually used by the WordPress community is wordpress_test, e.g.

bash
bin/install-wp-tests.sh wordpress_test root root

Please refer to the [Initialize the testing environment locally](http

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言