Download the PHP package sebkay/wp-queued-jobs without Composer
On this page you can find all versions of the php package sebkay/wp-queued-jobs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sebkay/wp-queued-jobs
More information about sebkay/wp-queued-jobs
Files in sebkay/wp-queued-jobs
Package wp-queued-jobs
Short Description A Laravel-like queue system for WordPress.
License MIT
Informations about the package wp-queued-jobs
WP Queued Jobs
A Laravel-like queue system for WordPress.
Easily create background jobs for things like sending emails or importing large amounts of data. All with a fluent API.
I highly recommend using a plugin like WP Crontrol so you can easily see, and manually run, cron jobs from the WordPress dashboard.
Requires PHP 7.4+
Install
The recommended way to install this package is via Composer.
Usage
1. Create job
To create a job, you need to extend the WpQueuedJobs\Jobs\Job
class:
2. Add job to queue and dispatch
Anything passed as the second parameter to addJob()
will be available in the handle()
method of the job (and the rest of the class) as $this->data
.
The data can be anything you want. A string
, array
, integer
, class
etc...
The "queue worker" will look for new jobs every 1 minute and run them (if there are any).
The system runs on a "first-in first-out" basis. So whatever gets dispatched first will be run first.
Important
As the lowest cron time in WordPress defaults to 1 minute, jobs scheduled to run at 6am might not actually run until 6:01am. This isn't an issue in the majority of cases, but it's worth knowing.
The point of background jobs is that they run in the background, not immediately, so they should never be used for time-sensitive tasks.
Example #1 (Send Email)
- Load the "successful registration" page template.
- Add the "send welcome email" job and dispatch it to the queue.
Example #2 (Import Posts from API)
- Create a custom cron event.
- Add two jobs and dispatch them to the queue.
- Each job is given an
offset
and amax
so the same posts aren't imported twice. - Schedule the cron even to run once per day at 6am, starting tomorrow.
Why
Unfortunatetly there's no concept of a "queue worker" in WordPress. At it's core WordPress is just a bunch of PHP files. There's no command line runner, such as Artisan in Laravel.
Although the WordPress CLI exists, a lot of people host their sites on shared hosting, so that isn't an option.
This package tries to circumvent that by utilising the WordPress Cron system. It runs a WP Cron task every minute to see if there are new jobs to run. If there are then it locks the connection to the database, runs the jobs, then unlocks the connection.
It's important to lock the queue to prevent the same jobs from running multiple times. The "queue worker" checks if there are new jobs every minute, so if any queue takes longer than a minute to finish jobs won't overlap.