Defining intervals, scheduling events, triggering tasks.
Document Header
Homepage:https://eacDoojigger.earthasylum.com/
Author:EarthAsylum Consulting
Last Updated:09-May-2025
Contributors:EarthAsylum Consulting, Kevin Burkholder
Requires {eac}Doojigger:3.1.2
WordPress URI:https://wordpress.org/plugins/search/earthasylum/
GitHub URI:https://github.com/EarthAsylum/docs.eacDoojigger/wiki/
Managing Recurring Events
Wordpress includes a few intervals, or schedules (hourly, twicedaily, daily, weekly), used to add timed, recurring events that get executed via the WordPress CRON system. These intervals, by themselves, don't do anything other than define an amount of time between the executions of a scheduled event.
For example, "Hourly" defines 3600 seconds as the interval between executions of an event that uses the "hourly" interval. To schedule an hourly event, you first create an event using wp_schedule_event()
, then hook an action to that event using add_action()
. When done properly, your action should be executed every hour.
Here is a simple example, setting a 'Daily' event...
$scheduledTime = new \DateTime( 'tomorrow 1am', wp_timezone() );
wp_schedule_event( $scheduledTime->getTimestamp(), 'daily', 'my_custom_daily_event' );
add_action('my_custom_daily_event', 'my_custom_daily_function');
The {eac}Doojigger Event Scheduler
(a.k.a. cron
) extension can't do much to make this any more simple, but it does add some nice shortcuts and features...
-
Adds a "Monthly" event that calculates the interval based on the number of days in the current month. When scheduling an event using "monthly", it should run on the sane day every month.
-
The ability to set or change the start time of any WP interval from the settings page, creating and scheduling a named event (prefixed with the plugin name).

-
Automatically check that a recurring event is actually scheduled and not inadvertently removed. Adds events when the plugin is (re)activated, and removes events when the plugin is deactivated.
-
Adds several methods to ease the creation and deletion of custom intervals, events, and tasks. Methods may be called via the extension alias...
eacDoojigger()->cron->{methodName}(...)
-
A custom interval is a named object defining a time period (in seconds) and a short description. Custom intervals are prefixed with the plugin name:
eacDoojigger_{intervalName}
.public function isInterval(string $name, int $interval = 0, string $display = ''): bool {...} public function getInterval(string $name, int $interval = 0, string $display = ''): array|bool {...} public function setInterval(string $name, int $interval, string $display): array {...} // unsetInterval() also removes any events that use this interval public function unsetInterval(string $name): array {...}
-
A custom event is a scheduled event executed by the WP-Cron system at specific times/intervals. If no interval name is given, the interval name is assumed to be the same as the event name. Custom events are prefixed with the plugin name and suffixed with '_event':
eacDoojigger_{eventName}_event
.public function isEvent(string $name): bool {...} public function getEvent(string $name): object|bool {...} public function setEvent(string $name, \DateTime|int|string $scheduledTime = null, string $interval = null, array $args = []): int|bool {...} public function unsetEvent(string $name): int|bool {...}
-
A custom task is a callback method assigned to run from a scheduled event (equivalent to a WordPress action). Custom task event names are prefixed with the plugin name and suffixed with '_event':
eacDoojigger_{eventName}_event
.public function isTask(string $eventName, callable $callback = false): bool {...} public function setTask(string $eventName, callable $callback, int $priority = 10, int $accepted_args = 1): bool {...} public function unsetTask(string $eventName, callable $callback, int $priority = 10): bool {...} // schedule the event (if not already) and hook a task to the event. public function addEventTask(string $eventName, callable $callback, int $priority = 10, int $accepted_args = 1): bool {...}
-
Examples
// Define our custom interval '{pluginname}_every_8_hours'. if (! eacDoojigger()->cron->isInterval('every_8_hours')) { eacDoojigger()->cron->setInterval('every_8_hours', 8*HOUR_IN_SECONDS, 'Every 8 Hours'); }
// run '{pluginname}_every_8_hours_event' based on the 'every_8_hours' interval // starting at 8pm this evening. if it is passed 8pm, calculate the next interval time. if (! eacDoojigger()->cron->isEvent('every_8_hours')) { eacDoojigger()->cron->setEvent('every_8_hours', '8pm'); }
// attach an action/task to the '{pluginname}_every_8_hours_event' event. if (! eacDoojigger()->cron->isTask('every_8_hours')) { eacDoojigger()->cron->setTask('every_8_hours', 'my_custom_function'); }
5. Adds a method to run (do) a single, non-recurring task either immediately or at a specified time. This method uses WP-Cron but spawns the request immediately, limiting WP-Cron to the single task.
```php
public function doTask(string $hook, array $args=[], \DateTime|int|string $scheduledTime = 'now'): bool
- Defines actions to trigger the above methods from outside code.
eacDoojigger()->do_action('add_cron_interval', 'every_8_hours', 8*HOUR_IN_SECONDS, 'Every 8 Hours'); eacDoojigger()->do_action('add_cron_event', 'every_8_hours', '8pm'); eacDoojigger()->do_action('add_cron_task', 'every_8_hours', 'my_custom_function');
eacDoojigger()->do_action('delete_cron_task', 'every_8_hours', 'my_custom_function'); eacDoojigger()->do_action('delete_cron_event', 'every_8_hours'); eacDoojigger()->do_action('delete_cron_interval', 'every_8_hours');
eacDoojigger()->do_action('add_event_task', 'daily', 'my_daily_function');
eacDoojigger()->do_action('do_cron_task', 'my_custom_action',[...]);
7. Adds a filter to limit the available/useable schedules.
```php
eacDoojigger()->add_filter('allowed_schedules', function($schedules)
{
unset($schedules['twicedaily'],$schedules['weekly'],$schedules['monthly']);
return $schedules;
}
);