1. Go to this page and download the library: Download rwsite/wp-queue library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rwsite / wp-queue example snippets
use WPQueue\Jobs\Job;
use WPQueue\Attributes\{Schedule, Queue, Timeout, Retries};
#[Schedule('hourly')]
#[Queue('imports')]
#[Timeout(120)]
#[Retries(5)]
class ImportProductsJob extends Job
{
public function __construct(
private array $productIds
) {
parent::__construct();
}
public function handle(): void
{
foreach ($this->productIds as $id) {
// Import logic here
}
}
public function failed(\Throwable $e): void
{
// Handle failure
error_log('Import failed: ' . $e->getMessage());
}
}
use WPQueue\WPQueue;
// Dispatch to queue
WPQueue::dispatch(new ImportProductsJob([1, 2, 3]));
// With delay (seconds)
WPQueue::dispatch(new ImportProductsJob($ids))->delay(60);
// To specific queue
WPQueue::dispatch(new ImportProductsJob($ids))->onQueue('high-priority');
// Synchronous (bypass queue)
WPQueue::dispatchSync(new ImportProductsJob($ids));
// Chain jobs
WPQueue::chain([
new FetchProductsJob(),
new ImportProductsJob($ids),
new NotifyAdminJob(),
])->dispatch();
// Batch jobs
WPQueue::batch([
new ImportProductJob($id1),
new ImportProductJob($id2),
new ImportProductJob($id3),
])->dispatch();
// In your plugin
add_action('wp_queue_schedule', function ($scheduler) {
// Using attributes (automatic)
$scheduler->job(ImportProductsJob::class);
// Manual scheduling
$scheduler->job(CleanupJob::class)->daily();
// Custom interval
$scheduler->job(CheckStopListJob::class)->everyMinutes(15);
// Conditional
$scheduler->job(SyncJob::class)
->hourly()
->when(fn() => get_option('sync_enabled'));
// From settings
$scheduler->job(BackupJob::class)
->interval(get_option('backup_interval', 'daily'));
// One-time execution (at specific timestamp)
$scheduler->job(OneTimeJob::class)->at(time() + 3600);
});
#[Schedule('hourly')] // Fixed interval
#[Schedule('daily', setting: 'my_opt')] // From wp_options
#[Queue('default')] // Queue name
#[Queue('high')] // Priority queue