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()
Note: These helpers do not validate hook callbacks; ensure you register your callback with add_action( $hook, ... ).
Note: These helpers do not validate hook callbacks; ensure you register your callback with add_action( $hook, ... ).
as_enqueue_async_action( string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): intEnqueue an action to run one time, as soon as possible.
$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).
Signature
int— Action ID on success;0on failure.
Parameters
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:
Returns
// 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.
}Filters
- 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.
as_schedule_single_action()
Example
Edge cases
as_schedule_single_action( int $timestamp, string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): intSchedule a one-time action to run at a specific timestamp.
- 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
- 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.
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
$timestamp = time() + HOUR_IN_SECONDS;
$action_id = as_schedule_single_action(
$timestamp,
'myplugin_send_report',
array( 'report_id' => 99 ),
'myplugin'
);Filters
int— Action ID on success;0on failure.
as_schedule_recurring_action()
Example
Edge cases
as_schedule_recurring_action( int $timestamp, int $interval_in_seconds, string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): intSchedule a recurring action to run on a fixed interval (in seconds).
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:
Signature
- 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.
Parameters
- 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.
Returns
// 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
);Filters
- 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_cron_action()
Example
Edge cases
as_schedule_cron_action( int $timestamp, string $schedule, string $hook, array $args = array(), string $group = '', bool $unique = false, int $priority = 10 ): intSchedule a recurring action based on a cron-like expression.
$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).
Signature
int— Action ID on success;0on failure.
Parameters
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:
Returns
// Run daily at 02:30 UTC.
$timestamp = time();
$cron = '30 2 * * *';
$action_id = as_schedule_cron_action(
$timestamp,
$cron,
'myplugin_daily_cleanup',
array(),
'myplugin'
);Filters
- 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.