Search by

concept-image / wp-cron

concept-image

WP-Cron jobs for Acorn themes and packages: a #[Schedule] attribute on hook methods, a reconciler that keeps the cron option in line with the code (changed, removed and disabled jobs), exception isolation, locks, and the cron:list, cron:sync and cron:run commands.

Package info

gitlab.com/concept-image-public/wp-cron/

Issues

pkg:composer/concept-image/wp-cron

Statistics

Installs: 775

Dependents: 2

Suggesters: 1

Stars: 0

3.1.0 2026-11-30 16:02 UTC

This package is auto-updated.

Last update: 2026-11-30 15:03:08 UTC


README

WP Cron

WP-Cron jobs for Acorn, declared with #[Schedule]

For Acorn themes and packages (Bedrock + Roots Acorn 6.4). Put #[Schedule] on a method or on an action class; the package hooks it, schedules its event, reschedules it when you change it, unschedules it when you remove it, and runs it with exception isolation and an optional lock.

Requirements

  • PHP 8.3+
  • Roots Acorn 6.4+
  • concept-image/wp-hook-attributes ^0.1 (installed with the package)

Installation

composer require concept-image/wp-cron

Themes on concept-image/wp-starter-theme already have it.

Usage

namespace App\Hooks;

use App\Services\PodcastImporter;
use ConceptImage\WpCron\Attributes\Schedule;
use ConceptImage\WpCron\Enums\Every;

final class PodcastHooks
{
    public function __construct(private readonly PodcastImporter $importer) {}

    #[Schedule('ci_podcast_import', Every::Daily, at: '03:00')]
    public function import(): void
    {
        $this->importer->importNewEpisodes();
    }

    #[Schedule('ci_podcast_sync', interval: 900, display: 'Every 15 minutes', withoutOverlapping: true)]
    public function sync(): void { … }
}

List the class in the theme's ThemeServiceProvider::$hooks (or register it with HookRegistrar::register() in any provider): #[Schedule] implements wp-hook-attributes' Hook interface, like #[Action].

Parameter
hookRequired, unique, [a-z0-9_]
recurrenceEvery::FifteenMinutes, Hourly, TwiceDaily, Daily, Weekly, Monthly (30 days), or a schedule name
interval, displayInstead of recurrence: seconds (60 or more) and the schedule label
atFirst run at 'HH:MM' in the site timezone; daily-or-longer jobs stay on that time across DST changes
withoutOverlapping, lockTtlSkip a run while the previous one still holds the lock (default TTL 3600 s)

#[WhenConfig] on the method or the class switches a job; a switched-off job is unscheduled.

Action classes

A standalone job can be a class of app/Actions (subdirectories included) with #[Schedule] on the class. It is found without being listed anywhere and runs its handle() method, else __invoke():

namespace App\Actions;

use ConceptImage\WpCron\Attributes\Schedule;
use ConceptImage\WpCron\Enums\Every;

#[Schedule('ci_some_action', Every::Daily, at: '03:00')]
final class SomeAction
{
    public function handle(PodcastImporter $importer): void { … } // dependencies injected
}

The class is resolved from the container only when the job runs. Same parameters, repeatable, #[WhenConfig] on the class; a class without a public non-static handle()/__invoke() is an InvalidSchedule. Choose an action class for one self-contained job, a Hooks method to keep a job next to related hooks. HookRegistrar reads method attributes only: on a Hooks class, #[Schedule] goes on the method. Discovery is configured by the actions key of config/wp-cron.php (path, namespace, extra classes, cache of the found list, on in production).

How events are kept in line

On init (last priority), the reconciler compares the declared jobs with the cron events: it schedules new jobs, reschedules jobs whose recurrence or time changed, realigns jobs moved by a DST change, and unschedules the jobs it scheduled before that are no longer declared. It only touches its own hooks (the wp_cron_owned option), never core or plugin events, and writes nothing when everything matches.

A job's exception is logged (Acorn log, [wp-cron]) and never breaks the WP-Cron run.

A mistake in a declaration (invalid #[Schedule] or config entry, a hook declared twice) throws an InvalidSchedule or DuplicateJob outside production, to fail fast. In production it never takes the site down: it is logged once per request, that declaration is skipped (the other jobs are declared) and the event already scheduled for its hook is kept. wp acorn cron:list shows it (skipped: …) and cron:sync exits 1.

Config jobs

For callbacks you can't decorate, publish config/wp-cron.php (wp acorn vendor:publish --tag=wp-cron-config):

return [
    'jobs' => [
        'ci_newsletter_live_link' => [
            'callback' => [App\Services\LiveNewsletterService::class, 'syncNextLiveLink'], // resolved when it runs
            'recurrence' => 'daily',
            'at' => '06:30',
        ],
    ],
    'recurrences' => [
        'every_ten_minutes' => ['interval' => 600, 'display' => 'Every 10 minutes'],
    ],
    'actions' => [
        'path' => app_path('Actions'),  // action classes, subdirectories included (null: off)
        'namespace' => 'App\\Actions',
        'classes' => [],                 // action classes outside 'path'
        'cache' => null,                 // cache the found list: null = production only
    ],
];

The 2.x config/cron.php still works, deprecated: see UPGRADE.

Commands

wp acorn cron:list                   # declared jobs, source, next run, status, skipped declarations
wp acorn cron:sync --dry-run         # preview; without --dry-run: apply (deploy scripts, exits 1 on an error)
wp acorn cron:run ci_podcast_import  # run one job now

Production

Set DISABLE_WP_CRON=true and run due events from a system cron:

*/5 * * * * wp cron event run --due-now --path=/path/to/site/web/wp --quiet

Claude Code

wp acorn vendor:publish --tag=wp-cron-ai (or wp-starter-theme's wp acorn starter:ai) publishes the wp-cron skill and rules to the Bedrock root's .claude/.

Development

composer ci   # Pint, Larastan (level max), Pest

Without PHP's dom/xml/mbstring extensions on the host: docker run --rm -u "$(id -u):$(id -g)" -v "$PWD":/app -w /app composer:2 composer ci.

Changelog

Please refer to CHANGELOG for more information.

Contributing

Please refer to CONTRIBUTING for more information.

License

Please refer to LICENSE for more information.

Credits