### Schedule Action with Arguments Source: https://actionscheduler.org/usage This example demonstrates how to schedule a single action with arguments and how the callback function should accept and use these arguments. ```php // You must specify the number of arguments to be accepted (in this case, 2). add_action( 'purchase_notification', 'send_purchase_notification', 10, 2 ); // When scheduling the action, provide the arguments as an array. as_schedule_single_action( time(), 'purchase_notification', array( 'bob@foo.bar', 'Learning Action Scheduler (e-book)', ) ); // Your callback should accept the appropriate number of parameters (again, in this case, 2). function send_purchase_notification( $customer_email, $purchased_item ) { wp_mail( $customer_email, 'Thank you!', "You purchased $purchased_item successfully." ); } ``` -------------------------------- ### Clean Failed Actions Using WP-CLI Source: https://actionscheduler.org/perf This example demonstrates how to use the WP-CLI command to clean up failed actions, specifying the status, batch size, and a time before which actions should be purged. ```bash // Example wp action-scheduler clean --status=failed --batch-size=50 --before='90 days ago' --pause=2 ``` -------------------------------- ### Ensure Recurring Action Remains Scheduled Using Hook Source: https://actionscheduler.org/usage This example demonstrates using the `action_scheduler_ensure_recurring_actions` hook to periodically check and reschedule a recurring action if it has been lost. ```php /** * Ensure recurring action remains scheduled. */ add_action( 'action_scheduler_ensure_recurring_actions', function() { if ( ! as_has_scheduled_action( 'my_plugin_recurring_action' ) ) { as_schedule_recurring_action( time(), HOUR_IN_SECONDS, 'my_plugin_recurring_action' ); } }); ``` -------------------------------- ### Get Next Scheduled Action Timestamp Source: https://actionscheduler.org/api Returns the timestamp for the next occurrence of a pending scheduled action. Useful for checking when an action is next due to run. ```php as_next_scheduled_action( $hook, $args, $group ); ``` -------------------------------- ### Enable Migration from Any Custom Datastore Source: https://actionscheduler.org/version3-0 Add this filter to allow Action Scheduler to initiate migration from any custom datastore, not just the internal WPPostStore. ```php add_filter( 'action_scheduler_migrate_data_store', '__return_true' ); ``` -------------------------------- ### Enqueue an action to run once, as soon as possible Source: https://actionscheduler.org/api Use `as_enqueue_async_action` to schedule a single action to run at the earliest opportunity. It accepts the hook name, arguments, an optional group, a boolean for uniqueness, and a priority. ```php as_enqueue_async_action( $hook, $args, $group, $unique, $priority ); ``` -------------------------------- ### Run Action Scheduler Queue Directly Source: https://actionscheduler.org/faq Initiate the Action Scheduler queue directly by calling this method. This is an alternative to relying on WP-Cron. ```php ActionScheduler::runner()->run(); ``` -------------------------------- ### Initiate Additional Loopback Requests for Runners Source: https://actionscheduler.org/perf This snippet demonstrates how to create multiple loopback requests to initiate additional queue runners when the 'action_scheduler_run_queue' action is triggered. It's useful for handling larger queues on powerful servers but should be used with caution due to potential site performance impacts. ```php /** * Trigger 5 additional loopback requests with unique URL params. */ function eg_request_additional_runners() { // allow self-signed SSL certificates add_filter( 'https_local_ssl_verify', '__return_false', 100 ); for ( $i = 0; $i < 5; $i++ ) { $response = wp_remote_post( admin_url( 'admin-ajax.php' ), array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => false, 'headers' => array(), 'body' => array( 'action' => 'eg_create_additional_runners', 'instance' => $i, 'eg_nonce' => wp_create_nonce( 'eg_additional_runner_' . $i ), ), 'cookies' => array(), )); } } add_action( 'action_scheduler_run_queue', 'eg_request_additional_runners', 0 ); /** * Handle requests initiated by eg_request_additional_runners() and start a queue runner if the request is valid. */ function eg_create_additional_runners() { if ( isset( $_POST['eg_nonce'] ) && isset( $_POST['instance'] ) && wp_verify_nonce( $_POST['eg_nonce'], 'eg_additional_runner_' . $_POST['instance'] ) ) { ActionScheduler_QueueRunner::instance()->run(); } wp_die(); } add_action( 'wp_ajax_nopriv_eg_create_additional_runners', 'eg_create_additional_runners', 0 ); ``` -------------------------------- ### Check Feature Support Source: https://actionscheduler.org/api Use `as_supports()` to determine if a specific feature is available in the current Action Scheduler version. This is useful for conditionally adding actions or implementing fallback logic. ```php if ( as_supports( 'ensure_recurring_actions_hook' ) ) { // Safe to depend on the 'action_scheduler_ensure_recurring_actions' hook. add_action( 'action_scheduler_ensure_recurring_actions', 'my_plugin_schedule_my_recurring_action' ); } ``` -------------------------------- ### as_enqueue_async_action() Source: https://actionscheduler.org/api Enqueues an action to run once as soon as possible. This function is suitable for immediate background processing tasks. ```APIDOC ## Function Reference / `as_enqueue_async_action()` ### Description Enqueue an action to run one time, as soon as possible. ### Usage ``` as_enqueue_async_action( $hook, $args, $group, $unique, $priority ); ``` ### Parameters * **$hook** (string)(required) Name of the action hook. * **$args** (array) Arguments to pass to callbacks when the hook triggers. Default: _`array()`_. * **$group** (string) The group to assign this job to. Default: _'’_. * **$unique** (boolean) Whether the action should be unique. Default: _`false`_. * **$priority** (integer) Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. ### Return value `(integer)` the action’s ID. Zero if there was an error scheduling the action. The error will be sent to `error_log`. ``` -------------------------------- ### Load Action Scheduler Library in WordPress Source: https://actionscheduler.org/usage This PHP code snippet demonstrates how to include the Action Scheduler library within your WordPress plugin or theme. It ensures the library is loaded before the 'plugins_loaded' hook with priority 0, which is crucial for its initialization. ```php latest_version(); return $args; } add_filter( 'as_async_request_queue_runner_post_args', 'eg_define_custom_user_agent', 10, 1 ); ``` -------------------------------- ### Increase Action Scheduler Time Limit Source: https://actionscheduler.org/perf Increases the maximum processing time for each request to 120 seconds (2 minutes). Use this if your host supports longer web request timeouts to speed up action processing. ```php function eg_increase_time_limit( $time_limit ) { return 120; } add_filter( 'action_scheduler_queue_runner_time_limit', 'eg_increase_time_limit' ); ``` -------------------------------- ### Disable Async Request Runner During Migration Source: https://actionscheduler.org/version3-0 Disable the async queue runner entirely while the migration is in process. Remember to remove this filter once the migration is complete. ```php add_filter( 'action_scheduler_allow_async_request_runner', '__return_false' ); ``` -------------------------------- ### Include Failed Status in Default Cleaner Purge Source: https://actionscheduler.org/perf This code snippet adds the 'failed' status to the list of statuses purged by the default Action Scheduler cleaner. This was the default behavior prior to version 3.9.4. ```php add_filter( 'action_scheduler_default_cleaner_statuses', function( $statuses ) { $statuses[] = ActionScheduler_Store::STATUS_FAILED; return $statuses; } ); ``` -------------------------------- ### as_unschedule_action() Source: https://actionscheduler.org/api Cancels the next occurrence of a specific scheduled action. ```APIDOC ## Function Reference / `as_unschedule_action()` ### Description Cancel the next occurrence of a scheduled action. ### Usage ``` as_unschedule_action( $hook, $args, $group ); ``` ### Parameters * **$hook** (string)(required) Name of the action hook. * **$args** (array) Arguments passed to callbacks when the hook triggers. Default: _`array()`_. * **$group** (string) The group the job is assigned to. Default: _'’_. ### Return value `(null)` ``` -------------------------------- ### Customize Failed Actions Retention Period Source: https://actionscheduler.org/perf This snippet shows how to change the default three-month retention period for failed actions. You can set a specific duration or retain them indefinitely by returning zero. ```php add_filter( 'action_scheduler_retention_period_for_failed', function( $retention_period ) { return 12 * MONTH_IN_SECONDS; } ); ``` ```php // or if you prefer to retain failed actions indefinitely add_filter( 'action_scheduler_retention_period_for_failed', '__return_zero' ); ``` -------------------------------- ### as_has_scheduled_action() Source: https://actionscheduler.org/api Checks if a specific scheduled action is currently in the queue. ```APIDOC ## Function Reference / `as_has_scheduled_action()` ### Description Check if there is a scheduled action in the queue, but more efficiently than as_next_scheduled_action(). It’s recommended to use this function when you need to know whether a specific action is currently scheduled. _Available since 3.3.0._ ### Usage ``` as_has_scheduled_action( $hook, $args, $group ); ``` ### Parameters * **$hook** (string)(required) Name of the action hook. Default: _none_. * **$args** (array) Arguments passed to callbacks when the hook triggers. Default: _`array()`_. * **$group** (string) The group the job is assigned to. Default: _'’_. ### Return value `(boolean)` True if a matching action is pending or in-progress, false otherwise. ``` -------------------------------- ### as_unschedule_all_actions() Source: https://actionscheduler.org/api Cancels all occurrences of a scheduled action. ```APIDOC ## Function Reference / `as_unschedule_all_actions()` ### Description Cancel all occurrences of a scheduled action. ### Usage ``` as_unschedule_all_actions( $hook, $args, $group ) ``` ### Parameters * **$hook** (string)(required) Name of the action hook. * **$args** (array) Arguments passed to callbacks when the hook triggers. Default: _`array()`_. * **$group** (string) The group the job is assigned to. Default: _'’_. ### Return value `(string|null)` The scheduled action ID if a scheduled action was found, or null if no matching action found. ``` -------------------------------- ### Cancel All Scheduled Actions Source: https://actionscheduler.org/api Cancel all occurrences of a scheduled action. Use this when you want to completely remove a recurring action from the schedule. ```php as_unschedule_all_actions( $hook, $args, $group ) ``` -------------------------------- ### Cancel Next Scheduled Action Source: https://actionscheduler.org/api Cancel only the next occurrence of a scheduled action. This is useful for preventing a single future run without affecting subsequent scheduled runs. ```php as_unschedule_action( $hook, $args, $group ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.