Note: There is already a detailed guideline on usage on Action Scheduler’s homepage.

Action Scheduler is developed by WooCommerce and used by popular plugins such as WooCommerce, Yoast SEO, WPForms, Email Notifications For WP ULike, etc.

Likewise, you can use Action Scheduler in your plugin. It can be used in many ways.

1. Using composer.json in your project repo:

"require": {
    "woocommerce/action-scheduler": "^3.1.6"
},

which upon running the command composer install, it will by default includes Action Scheduler under your plugin’s /wp-content/plugins/action-scheduler/. So, the folder structure would be your-plugin/wp-content/plugins/action-scheduler. If you want to customize this folder location, check out the Action Scheduler “wp-content” dubiety.

Once the Action Scheduler is included in your plugin, then you’ll need to include the main file. For example:

require_once __FILE__ .  '/wp-content/plugins/action-scheduler/action-scheduler.php';

2. Action Scheduler can also be used as a separate plugin. Download the latest stable release and install it like a normal plugin.


One-Time Schedule

Schedule an action to run as soon as possible. Processing the task in the background while still doing other things. For example the “Send Emails Asynchronously” option in Email Notifications For WP ULike plugin. This functionality of the plugin uses one-time scheduling tasks. When you click on the like button, the email is scheduled to be sent in the background while processing other tasks such as updating the vote count in the database, displaying the “thank you” message, etc. The other example is WPForms’s “Optimize Email Sending” option. The email is sent to the queue in the background while processing the form submission which means it doesn’t wait to send the email before processing the form submission.

Usage

Use the as_enqueue_async_action() function in your plugin where you want to schedule a task. For example: at this point, the plugin should send an email.

$one = 1;
$two = 2;
$three = 3;

as_enqueue_async_action( 'my_plugin_one_time_schedule_task', array( $one, $two, $three ), 'my_plugin' );

The first parameter is the name of the task, the second is the parameters to the callback function, third is the group name.

Think the above as a do_action(). Now we are using add_action().

add_action( 'my_plugin_one_time_schedule_task', 'my_plugin_process', 10, 3 );
function my_plugin_process( $one, $two, $three ) {
    // Process the one-time task.
}

That’s all about a one-time task.

Recurring Task

Schedule a task to run recursively. For example, the weekly summary email sent by WP Frontend Delete Account. The summary email about how many users deleted their accounts this past week. The email is sent weekly at the same time in the background.


Usage

Use the as_schedule_recurring_action() function in the plugin when you want to schedule a task to run weekly. For example: at this point, send an email weekly.

$one = 1;
$two = 2; 

if ( false === as_next_scheduled_action( 'my_plugin_weekly_task' ) ) {
        as_schedule_recurring_action( strtotime( '+ 7 days' ), WEEK_IN_SECONDS, 'my_plugin_weekly_task', array( $one, $two ), 'my_plugin' );
}

The if condition is to check if the task isn’t already scheduled. The first parameter is the timestamp when you want to run the task, the second is the name of the task, the third is the parameters to the callback function, and the fourth is the group name.

So, the add_action() would be:

add_action( 'my_plugin_weekly_task', 'my_plugin_process_weekely', 10, 2 );
function my_plugin_process_weekly( $one, $two) {
    // Process a task to run weekly.
}


That’s all. Check all other provided functions on Action Scheduler API docs.

Scheduling the tasks with Action Scheduler
Tagged on:

Sanjeev Aryal

Don't bury your thoughts, put your vision into reality ~ Bob Marley.

2 thoughts on “Scheduling the tasks with Action Scheduler

  • January 18, 2024 at 4:58 pm
    Permalink

    Sanjeev, thank you so much for your tutorial.
    I’m looking at this plugin to help me background a hook that performs custom actions after a form is submitted in gravity forms.
    The hook is gform_after_submission and it has a few parameters like $form and $entry .
    I use it a lot but I no want to background the function.
    I’m struggling to see how to use AS to help.

    I think I need to link this gravity forms hook (and its parameters) to the AS hook

    Do you think you could help me out with the first few lines ? I def need access to $entry and $form etc in my background process.

    Appreciate any help

    Reply
    • January 20, 2024 at 10:10 am
      Permalink

      Hi Adnaan – Thanks for reaching out.

      Unfortunately, I’m not fully aware with Gravity Forms hooks and how it’s operating things. I’d recommend contacting Gravity Forms support.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *