0

My plugin sets up a CRON job on plugin activation to run after every 5 minutes. I want to provide a section in my plugin settings page, where the user can specify an interval. How would I do it? I am using the cron_schedules filter to add a custom CRON schedule. I am thinking of using it to get the option value for the user set CRON interval. Is this the right way to do it? If not, then how should I do it?

Update:

//done in plugin activation
if ( !wp_next_scheduled( 'my_custom_event' ) ) {
    wp_schedule_event( time(), 'everyminute', 'my_custom_event' );
}

add_filter( 'cron_schedules', 'add_cron_schedule' );
add_action( 'my_custom_event',  'cron_data_update' );

//This is where I get the value set from the plugin settings
function add_cron_schedule( $schedules ) {
    $my_settings = get_option( 'my-settings' );
    $period = ( isset( $my_settings['my-cron-interval'] ) && $my_settings['my-cron-interval'] != '' ) ? $my_settings['my-cron-interval'] : 30;

    $schedules['everyminute'] = array(
        'interval' => MINUTE_IN_SECONDS * $period,
        'display' => __('My Schedule')
    );

    return $schedules;
}

function cron_data_update() {
    //do something
}

As you can see, I am getting the value set by the user via the plugin settings page. If nothing was set, I use 30 minutes as default. Is this the right way to do it?

  • edit your post to show the code you have tried even if it doesn't work – mmm Sep 21 '15 at 13:36
  • Sure. It works actually, but I am not sure if that is the right way to do. – Rutwick Gangurde Sep 22 '15 at 5:56
  • @mmm Done. Please check. – Rutwick Gangurde Sep 22 '15 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.