work with us

Scheduling tasks in WordPress

Written by Andrea Ariu: Web developer.
· 4 minute read

Recently I needed to schedule and periodically run a task in WordPress, and not having ever done anything like that, I had to take a look around and see what WordPress offered to do this.

WordPress supplies a function called “wp_schedule_event”, which allows you to schedule and periodically run, according to a certain frequency, custom tasks. Well, it seemed to cover all I needed. However, after doing some tests, I realised that my task wasn’t being executed according to the schedule time and the frequency I had set. That’s because WordPress runs scheduled tasks only when the website gets some traffic, which means, every time someone visits the website, WordPress checks if there are some tasks due to be executed, and if so, it runs them. That’s fine if you don’t need to run your task strictly (not even a second later) according to your desired frequency, and if your website doesn’t get much traffic as the server will not be overloaded by the operation of checking if some tasks are ready to be executed and running them. But what if your website does get much traffic?

I kept looking around, and I found out that you can set a flag called “DISABLE_WP_CRON” to true in your wp-config file, and WordPress will ignore its default “cron job logic”, unless the specific url “http:///wp-cron.php?doing_wp_cron” is visited. That refers to the core file which contains the scheduled tasks logic. So, in order to keep making WordPress run scheduled tasks, that url needs to be periodically visited. This can be achieved by setting up a cron job in your OS crontab file. You just need to type the command “crontab -e” in your terminal, and append your cron job to the crontab file. Your OS will periodically run a curl that visits the wp-cron.php url. This is what our cron job looks like:

0 * * * * curl http://example.com/wp-cron.php?doing_wp_cron

The part preceding the curl command refers to the frequency to run our cron job. In this case, it will be run every hour. Now, the server won’t be overloaded as the scheduled tasks check will be executed only once per hour. However, the timing issue still remains. In fact, we’re just telling WordPress to run its wp-cron file every hour, and no matter what the schedule time is and the frequency of your custom task, it will be executed along some other tasks, such as database maintenance ones, only after the curl is triggered. You might be thinking, well I could simply set my cron job frequency according to my needs for my custom task. Yes, you could. But what if you need to run your task every two minutes? The server would be overloaded again.

I eventually decided to put aside the WordPress Cron Job APIs and worked out a different way.

Unix-like operating systems supply a command called “at”, which allows you to run a task at a certain time only once. You can use the “at” command to append the job which starts running your custom task periodically to the crontab file. Like I said, I decided to completely shelve WP CRON, so the job you need to append will have nothing to do with WP CRON, but it will be a CURL which will trigger a wordpress action and run your custom task.

Here is our “at” command:

> at 23:50 jan 31
> (crontab -l && echo “*/5 * * * * curl -X POST http://example.com/wp-admin/admin-ajax.php -F action=my_task”) | crontab –
> ^d

Here is our wordpress action
add_action( ‘wp_ajax_nopriv_my_task’, do_my_task );
add_action( ‘wp_ajax_my_task’, ‘do_my_task’ );

function do_my_task() {
// Do Something
}

Our task will be first executed on January 31 at 23:50, and from then on, the operating system will run it every 5 minutes.

Conclusion

By following the above steps, we’ve finally sorted the “timing” issue! No matter when WordPress runs its wp-cron file, we now know when our task will be executed.