Scheduling Events in WordPress | WordPress Cron Jobs

Creating cron jobs in WordPress is actually really simple but yet, so many WordPress developers don’t understand WordPress cron jobs or they understand the concept of WordPress cron jobs but don’t know how to set them up appropriately.

This article is targeted to slightly more advanced WordPress developers while aiming to explain what WordPress cron jobs are, how they work, and how to use them appropriately.

Most of the content in this article was taken from an answer I originally posted on the WordPress Stack Exchange to the question “WP Cron desn’t execute when time elapses“.

What Are Cron Jobs?

I would like to clear the air a bit about WordPress cron jobs before moving forward.

Cron jobs are tasks that executed until completed at specified time intervals of time, which usually reoccur numerous times. Cron jobs are especially useful for automatically executing tasks that might have originally needed to be manually executed.

Web Server Cron Jobs vs. WordPress Cron Jobs

There is a difference between a cron job which is setup via your web hosting platform or web server and the cron jobs setup on WordPress. Cron jobs setup directly on the server are guaranteed to execute dead on the time intervals specified.

A WordPress cron job is exactly the same as a cron job setup via your web hosting platform or web server. They both execute tasks automatically for you at specified time intervals.

Executing Automated Tasks and Scheduling Events in WordPress with WordPress Cron Jobs

There is one draw back to using the WordPress cron jobs. WordPress cron jobs require a page visit to anywhere on your WordPress website in order for your tasks to be executed. That is because WordPress is a “pseudo-cron system”.

The positive to using WordPress cron jobs is that it’s incredibly simple to execute functionality specific to your WordPress website.

WordPress Cron Schedules

Firstly, define your custom WordPress cron job schedules using the WordPress filter hook cron_schedules.

Example: Defining a WordPress cron schedule every: 30 minutes, 1 hour, 2 hours.

Schedule WordPress Task at Specific Time Interval (WordPress Cron Schedule)

You need to decide where and when to actually schedule the event. Here is just an example snippet of code, which makes a call to a custom class method (This can be done with functions as well):

Here is the code which actually schedules the event:

Task Executed Automatically by WordPress at Specified Time Interval (WordPress Cron Schedule)

Now, all you need to do is make a call to the name of your custom cron task. In this example the cron task name is custom_imap_import.

So in this example, mbe_do_imap_import(); is called every 30 minutes (assuming you have enough traffic to your website).

Extra Information About Scheduling Events in WordPress using WordPress Cron Jobs

* Requires a page visit in order for your cron job to fire at correct times.

Example: If you scheduled a task at 30 minute intervals, but no one visits your site for 4 hours, your cron job won’t be fired until that visitor comes to your site 4 hours later. If you really truly need your task fired every 30 minutes, then it is advised to setup a legitimate cron job via your web hosting provider to visit your website at the desired intervals.

WordPress cron jobs don’t make your website slow!

Maybe you are thinking what if the cron-script takes a long time to be executed, will the visitors have to wait until the script is executed? Nope! How can that be possible? If you look at the wp-cron.php file you will find a line

ignore_user_abort(true);

It’s a php.ini configuration that sets that if you stop loading the site/script the script won’t stop executing.

If you look at the wp-includes/cron.php file you’ll find a line similar to this:

That means WordPress will wait only 0.01 second for triggering the execution then it will abort but as you have set ignore_user_abortto true the script will be executing. This functionality is a huge advantage to execute large scripts in WordPress cron jobs.

WordPress functions related to WordPress Cron Jobs

WordPress Stack Exchange

Remember, this article is just a summary of an answer to a question (WP Cron doesn’t execute when time elapses) I wrote on WordPress Stack Exchange.

If you have a question related to WordPress, you can asked it on WordPress Stack Exchange and someone will very likely provide an answer or solution to your question or issue.

If you don’t get an answer in somewhat of a timely manner, feel free to contact me with a link to your question on WordPress Stack Exchange and I will try to find time to answer it for you.

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] WP Cron doesn’t execute when time elapses asked by: @Mike Madern
  2. [WordPress Development] Cron jobs repeating themselves asked by: @urok93
  3. [WordPress Development] wp_schedule_event run in background or not? asked by: @Damithatt
  4. [WordPress Development] How do i schedule cron in wordpress for each second? asked by: @Frank
  5. [WordPress Development] Need to execute a cron job asked by: @Jose David Garcia Llanos
  6. [WordPress Development] Do WordPress cron jobs slow down page loading? asked by: @urok93
  7. [WordPress Development] Run function with linux cron job in WordPress asked by: @chap
  8. [WordPress Development] Multiple wp_schedule_event cron jobs in plugin cause multi-execution asked by: @Entrit
  9. [WordPress Development] How to have a cron execute at a certain time on a certain day of the week asked by: @Pete

Leave a Reply