0

For testing purposes I keep short time intervals for cron and when the functionality is working fine, I change it to the required time interval. Whenever I change the time intervals for ex: from 'three_days' to 'five_minutes' or from 'five_minutes' to 'fifteen_minutes', the cron is running with earlier frequency and not the updated one. I'm totally confused with this. Even after setting new intervals, cron function runs on previous time intervals only.

What might be the reason for this, kindly help me out on this.

This is my code:

add_filter('cron_schedules', 'filter_cron_schedules');
function filter_cron_schedules($schedules) {
    $schedules['fifteen_minutes'] = array(
        'interval' => 900, // seconds
        'display'  => __('Every 15 minutes') 
    );
    $schedules['twenty_minutes'] = array(
        'interval' => 1200, // seconds
        'display'  => __('Every 20 minutes') 
    );
    $schedules['three_days'] = array(
        'interval' => 259200, // seconds
        'display'  => __('Every 3 days') 
    );
    $schedules['five_minutes'] = array(
        'interval' => 300, // seconds
        'display'  => __('Every 5 minutes') 
    );
    return $schedules;
}
// Schedule the cron
add_action('wp', 'bd_cron_activation');
function bd_cron_activation() {
    if (!wp_next_scheduled('bd_cron_cache')) {
        wp_schedule_event(time(), 'twenty_minutes', 'bd_cron_cache'); // hourly, daily, twicedaily
    }
}
// Firing the function
add_action('bd_cron_cache', 'bd_data');
function bd_data() {
    // My Logic
}

2 Answers 2

2

Whenever I change the time intervals for ex: from 'three_days' to 'five_minutes' or from 'five_minutes' to 'fifteen_minutes', the cron is running with earlier frequency and not the updated one.

Because you only reschedule the event if it does not exist:

if ( ! wp_next_scheduled( 'bd_cron_cache' ) ) {
    // schedule
}

...which will always be false once you initially schedule the event. Instead, check the stored task's schedule against the value you actually want it to be:

if ( wp_get_schedule( 'bd_cron_cache' ) !== 'twenty_minutes' ) {
    // Above statement will also be true if NO schedule exists, so here we check and unschedule if required
    if ( $time = wp_next_schedule( 'bd_cron_cache' ) ) {
        wp_unschedule_event( $time, 'bd_cron_cache' );
    }
    wp_schedule_event( time(), 'bd_cron_cache', 'twenty_minutes' );
}
2
  • With some minor syntactical changes in your code it worked, thanks for sharing the valuable knowledge. Dec 9, 2015 at 10:22
  • No problem, please do share your changes/improvements with us. Dec 9, 2015 at 10:26
0
// Custom Cron Schedules
add_filter('cron_schedules', 'filter_cron_schedules');
function filter_cron_schedules($schedules) {
    $schedules['two_minutes'] = array(
        'interval' => 120, // seconds
        'display'  => __('Every 2 minutes') 
    );
    $schedules['twenty_minutes'] = array(
        'interval' => 1200, // seconds
        'display'  => __('Every 20 minutes') 
    );
    return $schedules;
}

// Schedule the cron
add_action('wp', 'bd_cron_activation');
function bd_cron_activation() {
    if ( wp_get_schedule('bd_cron_cache') !== 'two_minutes' ) {
            // Above statement will also be true if NO schedule exists, so here we check and unschedule if required
            if ( $time = wp_next_scheduled('bd_cron_cache'))
                wp_unschedule_event($time, 'bd_cron_cache');

            wp_schedule_event(time(),'two_minutes','bd_cron_cache');   
        }
}

// Firing the function
add_action('bd_cron_cache', 'bd_data');
function bd_data() {
    // My Logic
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.