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?