{"id":16173456,"url":"https://github.com/sebkay/wp-queued-jobs","last_synced_at":"2025-05-08T20:55:53.820Z","repository":{"id":37589406,"uuid":"491194410","full_name":"SebKay/wp-queued-jobs","owner":"SebKay","description":"A Laravel-like queue system for WordPress.","archived":false,"fork":false,"pushed_at":"2024-11-07T09:54:11.000Z","size":320,"stargazers_count":39,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-03T05:08:22.040Z","etag":null,"topics":["background-jobs","queue","queues","wordpress","wordpress-api","wordpress-background-jobs","wordpress-php-library","wordpress-queue","wp"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SebKay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-11T16:40:19.000Z","updated_at":"2025-03-25T17:54:25.000Z","dependencies_parsed_at":"2024-02-02T08:47:49.357Z","dependency_job_id":"9503f3db-8b58-4c31-b57b-c2644a2a81e7","html_url":"https://github.com/SebKay/wp-queued-jobs","commit_stats":{"total_commits":29,"total_committers":4,"mean_commits":7.25,"dds":0.4482758620689655,"last_synced_commit":"b3a2b014007ad8d485b99d821fec4266104934c8"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebKay%2Fwp-queued-jobs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebKay%2Fwp-queued-jobs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebKay%2Fwp-queued-jobs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebKay%2Fwp-queued-jobs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SebKay","download_url":"https://codeload.github.com/SebKay/wp-queued-jobs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253149404,"owners_count":21861718,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background-jobs","queue","queues","wordpress","wordpress-api","wordpress-background-jobs","wordpress-php-library","wordpress-queue","wp"],"created_at":"2024-10-10T04:08:47.482Z","updated_at":"2025-05-08T20:55:53.803Z","avatar_url":"https://github.com/SebKay.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WP Queued Jobs\n\n[![Validate PHP](https://github.com/SebKay/wp-queued-jobs/actions/workflows/validate-php.yml/badge.svg)](https://github.com/SebKay/wp-queued-jobs/actions/workflows/validate-php.yml)\n\nA [Laravel](https://laravel.com/)-like queue system for [WordPress](https://wordpress.org/).\n\nEasily create background jobs for things like sending emails or importing large amounts of data. All with a fluent API.\n\nI highly recommend using a plugin like [WP Crontrol](https://wordpress.org/plugins/wp-crontrol/) so you can easily see, and manually run, cron jobs from the WordPress dashboard.\n\n**Requires PHP 7.4+**\n\n***\n\n## Install\n\nThe recommended way to install this package is via [Composer](https://getcomposer.org/).\n\n```shell\ncomposer require sebkay/wp-queued-jobs\n```\n\n## Usage\n\n### 1. Create job\n\nTo create a job, you need to extend the `WpQueuedJobs\\Jobs\\Job` class:\n\n```php\nuse WpQueuedJobs\\Jobs\\Job;\n\nclass BackgroundJob extends Job\n{\n    public function handle()\n    {\n        // Handle the job\n        // Use $this-\u003edata to access what was passed with the job when it was added to the queue\n    }\n}\n```\n\n### 2. Add job to queue and dispatch\n\n```php\nwpj()\n    -\u003eaddJob(BackgroundJob::class, 'Data for the background job.')\n    -\u003edispatch();\n```\n\nAnything 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-\u003edata`.\n\nThe data can be anything you want. A `string`, `array`, `integer`, `class` etc...\n\nThe \"queue worker\" will look for new jobs every 1 minute and run them (if there are any).\n\nThe system runs on a \"first-in first-out\" basis. So whatever gets dispatched first will be run first.\n\n#### Important\n\nAs 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.\n\nThe point of background jobs is that they run in the background, not immediately, so they should never be used for time-sensitive tasks.\n\n## Example #1 (Send Email)\n\n1. Load the \"successful registration\" page template.\n2. Add the \"send welcome email\" job and dispatch it to the queue.\n\n```php\nadd_action('template_redirect', function () {\n    if (!is_page_template('register-success.php')) {\n        return;\n    }\n\n    wpj()\n        -\u003eaddJob(SendWelcomeEmailJob::class, wp_get_current_user())\n        -\u003edispatch();\n}, 10, 0);\n```\n\n## Example #2 (Import Posts from API)\n\n1. Create a custom cron event.\n2. Add two jobs and dispatch them to the queue.\n3. Each job is given an `offset` and a `max` so the same posts aren't imported twice.\n4. Schedule the cron even to run once per day at 6am, starting tomorrow.\n\n```php\nadd_action('import_api_posts', function () {\n    wpj()\n        -\u003eaddJob(ImportApiPostsJob::class, ['offset' =\u003e 0, 'max' =\u003e 100])\n        -\u003eaddJob(ImportApiPostsJob::class, ['offset' =\u003e 100, 'max' =\u003e 100])\n        -\u003edispatch();\n}, 10, 0);\n\nif (!\\wp_next_scheduled('import_api_posts')) {\n    \\wp_schedule_event(\n        \\strtotime(\"+ 1 days 6am\"),\n        'daily',\n        'import_api_posts'\n    );\n}\n```\n\n## Why\n\nUnfortunatetly 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](https://laravel.com/docs/9.x/artisan) in Laravel.\n\nAlthough the [WordPress CLI](https://wp-cli.org/) exists, a lot of people host their sites on shared hosting, so that isn't an option.\n\nThis 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.\n\nIt'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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebkay%2Fwp-queued-jobs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebkay%2Fwp-queued-jobs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebkay%2Fwp-queued-jobs/lists"}