WP Cron Doesn't Execute When Time Elapses

by Mike Madern   Last Updated April 15, 2019 23:08 PM

The goal

I want to use wp_schedule_single_event( ) to execute a single event that sends me an e-mail 8 minutes after the user submits a form.

The issue

The following code is in my functions.php:

function nkapi_send_to_system( $args ) {
  wp_mail( 'xxx', 'xxx', $args );
}

add_action( 'nkapi_send', 'nkapi_send_to_system' );

function schedule_event( $id ) {
  wp_schedule_single_event( current_time( 'timestamp' ) + 480, 'nkapi_send', array( $id ) );
}

And the following code is used to call schedule-event:

schedule_event( $_SESSION['insert_id'] ); // the $_SESSION var contains an INT

After waiting some more than 8 minutes there wasn't an e-mail in my inbox.

What I tried

With the plugin Core Control it's possible to see wich cron jobs are scheduled.

Core Control screen

After a couple of changes I managed to get them quite correct, and better, when I hit "Run Now", I actually get an e-mail in my inbox.

But why the cron's don't execute when I visit my site after 8 minutes. What possibly is wrong with this code? I have to say that this is my first time using WP Cron.

I tried more

After the comment of vancoder id decided to test if the code works if I put the following code directly in the functions.php:

function schedule_event( $id ) {
  wp_schedule_single_event( time(), 'nkapi_send', array( $id ) );
}

if ( isset( $_SESSION['insert_id'] ) ) {
  if ( ! array_key_exists( 'insert_scheduled', $_SESSION ) || $_SESSION['insert_scheduled'] != $_SESSION['insert_id'] ) {
    schedule_event( $_SESSION['insert_id'] );
    $_SESSION['insert_scheduled'] = $_SESSION['insert_id'];
  }
}

The downside of this code is, the user has to go to another page before this code is executed. But on the other side, this doesn't work either so that wouldn't be my first issue...



Answers 5


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:

Michael Ecklund
Michael Ecklund
April 04, 2013 14:22 PM

Check that DISABLE_WP_CRON is not set in your config.

Failing that, try disabling all plugins (except core control - though I would use wp-crontrol) and see if your core jobs work. If they do, you are experiencing plugin interference somewhere.

Similarly, try switching to a standard twentysomething theme.

If none of these make any difference, chances are it's a hosting issue.

vancoder
vancoder
April 04, 2013 17:19 PM

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!

WPMU-DEV Ari
WPMU-DEV Ari
April 05, 2013 14:59 PM

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 
doublesharp
doublesharp
April 06, 2013 00:43 AM

Check any plugin that hides Wordpress.

How to see if this is the problem?

  1. Navigate to http(s)://yoursite.com/wp-cron.php You should see an empty page. A completely empty one.
  2. Besides you must see in a cron job manager a time under "Next execution": Cron job scheduled - if wp-cron.php works properly (not only the text "In queue" - but a given time - for some entries "In queue" is OK sometimes, but if it is the only thing you see -> your cron does not work.)

+1. Do not believe any plugins that "check if cron is working" - e.g. WP Cron status checker plugin has shown that cron is working. But in fact it did not. Whatever it shows - believe your eyes and not this plugin!

Conclusion: If it is 404 error - then switch off a) not only caching plugins as others suggest b) but also any plugins that hide Wordpress.

Peter Cz
Peter Cz
April 15, 2019 21:45 PM

Related Questions


Updated September 08, 2016 08:05 AM

Updated May 13, 2016 08:03 AM

Updated September 05, 2017 04:08 AM

Updated December 28, 2016 08:03 AM