arraypress / wp-register-cron
Declarative WordPress cron jobs and custom schedules, cleaned up properly on uninstall.
Requires
- php: >=8.3
Requires (Dev)
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.13.5
- wp-coding-standards/wpcs: ^3.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Declare a plugin's scheduled jobs, and have them cleaned up properly when it is removed.
What it does
Scheduling in WordPress is two calls that must not disagree:
wp_next_scheduled() before wp_schedule_event(), on the right hook, with
the right arguments — and then the same arguments again at uninstall or the
job is left behind forever, firing for a plugin that is no longer there.
This is the declaration instead. You say what should run and how often; the scheduling, the duplicate check and the uninstall are handled.
Features
- Declare a job once, instead of guarding
wp_schedule_event()by hand - Add a schedule WordPress does not have, like every five minutes
- Pass arguments, so the same hook can run per site, feed or account
- Unschedule everything the plugin registered, in one call at uninstall
- Ask when a job runs next, to show in an admin screen
- Change a schedule and have the existing event rescheduled, not duplicated
Installation
composer require arraypress/wp-register-cron
Quick start
add_action( 'init', function () { register_cron_job( 'myplugin_sync', [ 'schedule' => 'daily', 'callback' => 'myplugin_run_sync', ] ); } );
A schedule of your own, if hourly, twicedaily and daily are not enough:
register_cron_schedule( 'every_five_minutes', 5 * MINUTE_IN_SECONDS, __( 'Every five minutes', 'my-plugin' ) );
And at uninstall, the part that usually gets forgotten:
unregister_cron_jobs();
What it does not do
WordPress cron runs on traffic, not on time — a quiet site runs a "daily" job
late, and a job that must be punctual needs a real system cron hitting
wp-cron.php. Nothing here changes that.
Requirements
- PHP 8.3 or later
- WordPress 7.1 or later
License
GPL-2.0-or-later