Scheduling actions
This document covers the public scheduling helpers exposed by Action Scheduler.
Overview
Use these functions to enqueue or schedule actions to run now, once in the future, on a fixed interval, or on a cron-like schedule. All functions return the scheduled action ID on success and 0 on failure.
Common concepts:
- Hook: The action name that will be triggered when the job runs.
- Args: Array of arguments passed to the hook callback.
- Group: A string for grouping related actions.
- Unique: When
true, an action is only scheduled if there is not already a pending or running action with the same hook and group. - Priority: Lower values run first. Accepted values:
0–255. Default:10.
Note: These helpers do not validate hook callbacks; ensure you register your callback with
add_action( $hook, ... ).
as_enqueue_async_action()
Enqueue an action to run one time, as soon as possible.
Signature
as_enqueue_async_action( string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): int
Parameters
$hook(string) — Hook to trigger.$args(array) — Arguments passed to the hook callback.$group(string) — Group name for the action.$unique(bool) — Iftrue, do not enqueue if a pending or running action with the same hook and group already exists.$priority(int) — Lower values run first; default10(range0–255).
Returns
int— Action ID on success;0on failure.
Filters
pre_as_enqueue_async_action— Short-circuit scheduling.- Signature:
apply_filters( 'pre_as_enqueue_async_action', ?int $pre, string $hook, array $args, string $group, int $priority, bool $unique ) - Return a non-
nullinteger to bypass the default scheduling logic. Any non-int return is treated as0.
- Signature:
Example
// Enqueue an async action to run ASAP.
$action_id = as_enqueue_async_action(
'myplugin_process_queue',
array( 'queue_id' => 42 ),
'myplugin',
true
);
if ( 0 === $action_id ) {
// Scheduling failed.
}
Edge cases
- If Action Scheduler is not initialized, returns
0. - When
$uniqueistrue, uniqueness is scoped to hook + group (not args). - Returning any non-
nullvalue from the filter bypasses the default logic, so ensure your filter returns a valid action ID or0.
as_schedule_single_action()
Schedule a one-time action to run at a specific timestamp.
Signature
as_schedule_single_action( int $timestamp, string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): int
Parameters
$timestamp(int) — Unix timestamp when the action should run (UTC).$hook(string) — Hook to trigger.$args(array) — Arguments passed to the hook callback.$group(string) — Group name for the action.$unique(bool) — Iftrue, do not schedule if a pending or running action with the same hook and group already exists.$priority(int) — Lower values run first; default10(range0–255).
Returns
int— Action ID on success;0on failure.
Filters
pre_as_schedule_single_action— Short-circuit scheduling.- Signature:
apply_filters( 'pre_as_schedule_single_action', ?int $pre, int $timestamp, string $hook, array $args, string $group, int $priority, bool $unique ) - Return a non-
nullinteger to bypass the default scheduling logic. Any non-int return is treated as0.
- Signature:
Example
$timestamp = time() + HOUR_IN_SECONDS;
$action_id = as_schedule_single_action(
$timestamp,
'myplugin_send_report',
array( 'report_id' => 99 ),
'myplugin'
);
Edge cases
- If Action Scheduler is not initialized, returns
0. - If
$timestampis in the past, Action Scheduler will run it as soon as possible. - When
$uniqueistrue, uniqueness is scoped to hook + group (not args).
as_schedule_recurring_action()
Schedule a recurring action to run on a fixed interval (in seconds).
Signature
as_schedule_recurring_action( int $timestamp, int $interval_in_seconds, string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): int
Parameters
$timestamp(int) — Unix timestamp for the first run (UTC).$interval_in_seconds(int) — Interval between runs, in seconds.$hook(string) — Hook to trigger.$args(array) — Arguments passed to the hook callback.$group(string) — Group name for the action.$unique(bool) — Iftrue, do not schedule if a pending or running action with the same hook and group already exists.$priority(int) — Lower values run first; default10(range0–255).
Returns
int— Action ID on success;0on failure.
Filters
pre_as_schedule_recurring_action— Short-circuit scheduling.- Signature:
apply_filters( 'pre_as_schedule_recurring_action', ?int $pre, int $timestamp, int $interval_in_seconds, string $hook, array $args, string $group, int $priority, bool $unique ) - Return a non-
nullinteger to bypass the default scheduling logic. Any non-int return is treated as0.
- Signature:
Example
// Run every 15 minutes, starting at the next quarter hour.
$timestamp = time() + ( 15 * MINUTE_IN_SECONDS );
$interval = 15 * MINUTE_IN_SECONDS;
$action_id = as_schedule_recurring_action(
$timestamp,
$interval,
'myplugin_sync_catalog',
array( 'source' => 'api' ),
'myplugin',
true
);
Edge cases
- If
$interval_in_secondsis not numeric or is not an integer value (even if numeric), Action Scheduler triggers_doing_it_wrong()and returns0. - Recurring actions are scheduled sequentially. Each next occurrence is scheduled after the previous run completes.
- When
$uniqueistrue, uniqueness is scoped to hook + group (not args).
as_schedule_cron_action()
Schedule a recurring action based on a cron-like expression.
Signature
as_schedule_cron_action( int $timestamp, string $schedule, string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): int
Parameters
$timestamp(int) — Base timestamp. The first run is calculated after this time and must match the cron expression.$schedule(string) — Cron-like schedule string.$hook(string) — Hook to trigger.$args(array) — Arguments passed to the hook callback.$group(string) — Group name for the action.$unique(bool) — Iftrue, do not schedule if a pending or running action with the same hook and group already exists.$priority(int) — Lower values run first; default10(range0–255).
Returns
int— Action ID on success;0on failure.
Filters
pre_as_schedule_cron_action— Short-circuit scheduling.- Signature:
apply_filters( 'pre_as_schedule_cron_action', ?int $pre, int $timestamp, string $schedule, string $hook, array $args, string $group, int $priority, bool $unique ) - Return a non-
nullinteger to bypass the default scheduling logic. Any non-int return is treated as0.
- Signature:
Example
// Run daily at 02:30 UTC.
$timestamp = time();
$cron = '30 2 * * *';
$action_id = as_schedule_cron_action(
$timestamp,
$cron,
'myplugin_daily_cleanup',
array(),
'myplugin'
);
Edge cases
- Cron actions are scheduled sequentially, just like recurring actions. Only the next occurrence is stored at any time.
- The first run will always be after
$timestampand must match the cron expression. - When
$uniqueistrue, uniqueness is scoped to hook + group (not args).