WordPress Cron is a useful functionality that is provided by WordPress since 2.1. This simulates an actual cron job and gives the scheduling ability to plugins or themes without requiring regular users to set real cron jobs on their servers. In this tutorial we will look into different WordPress Cron functions and learn how to use them.

I have used WordPress Cron in my own plugins. My plugin GiveASAP for hosting giveaways, uses this functionality to schedule various events such as the ending of a giveaway. When the end date is met, the giveaway will stop accepting new emails.

What is WordPress Cron?

A regular cron job, as stated on Wikipedia, is:

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

WordPress Cron is a little bit different. It is a scheduler but not in the sense of periodically running scripts or commands on the server. On your server, you could create cron jobs that will execute a file on your server or similar. This will all be done using some server commands and it is not dependant of your website or visitors.

A WordPress Cron will use WordPress functionalities and it will run action hooks that are scheduled using WordPress Cron. After that, every function that is hooked to the scheduled action will be called and executed.

WordPress Cron is dependant on the website and the visitors. It will not execute the functions on the exact time unless a visitor has loaded your website at that time. When a visitors opens your website, WordPress Cron will check the unix time. If there are scheduled events that have to run on that time, they will get executed. Scheduled events that had to be run previously but they did not, will also get executed.

By doing that, WordPress Cron simulates the Cron functionality and enables us to schedules various events such as:

  • Post Publishing (Scheduling)
  • Scheduling a Backup
  • Periodically Sending Emails about our Site Status

We will cover some of the WordPress Cron functions that you could use for your own WordPress solutions.

Scheduling a Single Event

When scheduling a single event, we are making sure that this event will happen only once. A known scenario for scheduling single events is the Post Scheduling. When you schedule a post, an event will be scheduled using the date & time that we define and the ID of the post.

wp_schedule_single_event( $timestamp, $hook, $args = array() );

Since the function is receiving a parameter $args, you can pass an array with as many configuration options as you want. You must remember that how you define the array, you must use it the same way if you want to unschedule this event.

Let’s say that we have a Post or a Custom Post Type that we want to share on social media after an hour of publishing. We would do something like this:

I hope you don’t mind it. I did complicate it a bit, but I think it will make you understand it better. So, the first thing we do here is to hook our function when our post is published. This is done by using the action publish_post. If you are using a custom post type, just replace post with your custom post type slug.

Our function share_my_post_after_hour will then get the timestamp of our post. After that we will add an hour (in seconds) to it by using WordPress Time Constants. We then build the rest of our parameters which is the variable $args that will hold the post ID and the variable $hook that will hold the name of our event action.

Once our event is fired, that event action share_the_post_after_hour will be called as an action hook do_action('share_the_post_after_hour'); and all functions that are hook to it will be processed.

We are then hooking a function process_sharing_for_post to that action. If we had the logic behind that function, we would have shared our post.

Scheduling Events

Instead of scheduling one single event, we can schedule events that will occur periodically. When scheduling such an event, we have to pass the timestamp, interval, event action and arguments (if any).

wp_schedule_event( $timestamp, $interval, $hook, $args );

WordPress, by default, defines three intervals:

  • hourly – an event will occur every hour
  • twicedaily – an event will occur twice a day (every 12 hours)
  • daily – an event will occur once a day (every 24 hours)

Custom WordPress Cron Intervals

We can, of course, define our own intervals. This is done like this:

In our function we are adding a new key every_six_hours to our schedule and we are assigning an array with the interval in seconds and a description for it.

Scheduling Example

I will follow the last example and create an example where we will schedule an event to check and update the share counts for our published post. We will do that like this:

As in the previous example, we are hooking a function to the action that will occur when we publish our post. We are here getting the timestamp in the same way and also checking for an already scheduled event.

How to get the Share Counts from Social Media in WordPressIf there is no event, we will schedule one that will call the event action every_six_hours. We have hooked a function just as an example but the logic of that function is out of the scope for this tutorial. To see how to get share counts for your post you can check my article How to get the Share Counts from Social Media in WordPress.

How to Unschedule an Event

To unschedule an event you have to provide the exact timestamp on which the event will occur next, the event action and also any arguments that we passed when scheduling the event. The function for unscheduling the event is:

wp_unschedule_event( $timestamp, $hook, $args );

To be consistent, I will use the sharing example again. For this example, we will unschedule our sharing event for a post when we move the post to trash.

We have used the hook trashed_post because that action hook is called once the post has been trashed. That action also passes the post ID so we can easily create the same argument array as we did when scheduling the event.

If you don’t pass the same argument list, WordPress Cron function wp_next_scheduled will not the exact timestamp and you won’t be able to unschedule the event.

We are also getting the next timestamp when the event has to occur. If the $timestamp is not false we will unschedule the event for that post ID.

Using Server/System Cron instead of WordPress Cron

Since WordPress Cron does not run continuously because it needs the website to load first, it is dependant on your visitors. If you want a continuously running Cron job, you will have to use your server cron job.

This can be be useful for example: emailing your users 15min before an event, webinar or simple. For that to be able to do using WordPress Cron, you would need your website to load every minute so that WordPress can check if any event has been scheduled 15min onwards.

You need to set the cron job on your server which on most hosts you can do using a cPanel or similar management system. When setting the cron job you have to point the file which has to be executed every X time. The file is located http://your_domain.com/wp-cron.php.

If you don’t have a cPanel or something similar, you probably have access to your server using ssh or similar. You can then set the cron job using some OS commands. Read this for more information: Hooking WP Cron into the System Task Scheduler.

Conclusion

WordPress Cron is a great feature that you can use to schedule various events on your WordPress website. With some simple examples, we have seen how to use the WordPress Cron for some sharing functionality. This can be also used for various other scenarios.

Have you had some situations where you had to use WordPress Cron? Have you developed a plugin that uses it? Please do share in the comments below:)

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

One Comment

  1. one of the best tutorials, thanks!

    Reply

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.