I want users of my plugin to be able to define whether a task operates daily, twice daily, hourly or not at all as per the standard WP set-up. I'm storing these as 'daily', 'twicedaily', 'hourly' or '' in the option obr_scheduled_interval.
The problem is that the task that I want 'obr_grab' runs every time there's a page visit rather than every time there's a page visit after the selected time period.
These are the relevant bits of code from the class.
function __construct(){
register_activation_hook(__FILE__, array(&$this, 'obr_activate_scheduled_task'));
add_action('obr_scheduled_task', array(&$this, 'obr_activate_scheduled_task'));
add_shortcode('obr_grab', array(&$this, 'obr_grab'));
register_deactivation_hook(__FILE__, array(&$this, 'obr_deactivate_scheduled_task'));
}
function obr_activate_scheduled_task() {
// function to activate the scheduled task (if there is one);
if (wp_next_scheduled('obr_scheduled_task')){
wp_clear_scheduled_hook('obr_scheduled_task');
}
$interval = get_option('obr_scheduled_interval');
if (strlen($interval) > 0){
wp_schedule_event(time(), $interval, 'obr_scheduled_task');
}
}
function obr_scheduled_task(){
// if called we need to use the grab function
$this->obr_grab();
}
function obr_deactivate_scheduled_task(){
// clear the schedule on deactivation
wp_clear_scheduled_hook('obr_scheduled_task');
}
I've looked at core control but there are only two standard jobs there: wp_scheduled_auto_draft_delete and wp_scheduled_delete
All help much appreciated!