Wordpress - WP Cron Doesn't Execute When Time Elapses

Firstly, define your custom cron job schedules.

add_filter('cron_schedules', array($this, 'cron_schedules'));

public function cron_schedules($schedules){
    $prefix = 'cron_';// Avoid conflict with other crons. Example Reference: cron_30_mins
    $schedule_options = array(
        '30_mins' => array(
            'display' => '30 Minutes',
            'interval' => '1800'
        ),
        '1_hours' => array(
            'display' => 'Hour',
            'interval' => '3600'
        ),
        '2_hours' => array(
            'display' => '2 Hours',
            'interval' => '7200'
        )
    );
    /* Add each custom schedule into the cron job system. */
    foreach($schedule_options as $schedule_key => $schedule){
        $schedules[$prefix.$schedule_key] = array(
            'interval' => $schedule['interval'],
            'display' => __('Every '.$schedule['display'])
        );
     }
     return $schedules;
}

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:

$schedule = $this->schedule_task(array(
    'timestamp' => current_time('timestamp'), // Determine when to schedule the task.
    'recurrence' => 'cron_30_mins',// Pick one of the schedules set earlier.
    'hook' => 'custom_imap_import'// Set the name of your cron task.
));

Here is the code which actually schedules the event:

private function schedule_task($task){
    /* Must have task information. */
    if(!$task){
        return false;
    }
    /* Set list of required task keys. */
    $required_keys = array(
        'timestamp',
        'recurrence',
        'hook'
    );
    /* Verify the necessary task information exists. */
    $missing_keys = array();
    foreach($required_keys as $key){
        if(!array_key_exists($key, $task)){
            $missing_keys[] = $key;
        }
    }
    /* Check for missing keys. */
    if(!empty($missing_keys)){
        return false;
    }
    /* Task must not already be scheduled. */
    if(wp_next_scheduled($task['hook'])){
        wp_clear_scheduled_hook($task['hook']);
    }
    /* Schedule the task to run. */
    wp_schedule_event($task['timestamp'], $task['recurrence'], $task['hook']);
    return true;
}

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.

add_action('custom_imap_import', array($this, 'do_imap_import'));

public function do_imap_import(){
    // .... Do stuff when cron is fired ....
}

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


Notes

Requires a page visit in order for your cron 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 like this:

wp_remote_post( $cron_url, 
array('timeout' => 0.01,
 'blocking' => false, 
 'sslverify' => apply_filters('https_local_ssl_verify', true)) );

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

Functions available for aid:

  • wp_schedule_event
  • wp_schedule_single_event
  • wp_clear_scheduled_hook
  • wp_next_scheduled
  • wp_unschedule_event
  • wp_get_schedule

First can you please confirm that you don't have any caching plugins enabled? Caching plugins can interfere with cron jobs because your visitors are not served a live page but a cached version of your page.

If you have a caching plugin enabled, you can choose one of your pages, add an exclussion to your caching plugin's settings for that page so that it is never cached.

Then you'll have to manually create a cron job (using cpanel if you're on a shared hosting environment or from the terminal if it's a VPS/dedicated server) that will visit that page every few minutes.

I hope that helps!


WordPress Cron allows you to schedule tasks, but they will only execute if there is a request made to the site. For each request that WordPress receives it will check to see if there are cron jobs to process, and if so fires off a request to /wp-cron.php?doing_wp_cron asynchronously to process the job. If a job's scheduled start passes without a request, then the cron process won't be started.

Since you are able to see and run your scheduled jobs, it's possible that there are no requests that trigger the cron job to start, especially if you're using a caching plugin. The best option for offloading this to a more regular schedule is to disable the default check in WordPress and use crontab.

First to disable the default check (which can help a bit with client side performance), add the following to wp-config.php:

// Disable default check for WordPress cron jobs on page loads
define( 'DISABLE_WP_CRON', true );

Next you create a task to fetch the wp-cron.php page once a minute to process any jobs on the back end, from the command line enter crontab -e and then add a line that looks like the following:

*/1 * * * * /usr/bin/curl --silent http://example.com/wp-cron.php?doing_wp_cron=$(date +\%s.\%N) >/dev/null