Get frequency of scheduled event

How do I get the frequency of a scheduled event?

I am writing a plugin, that would do something in a user defined schedule.

Read More

The way I am trying to do this, is creating a custom cron schedule, which take a user input value as the interval.

This is how I do it. Please note that everything is in classes.

class classA{
    private function createCustomTimeFrame() {
        add_filter( 'cron_schedules', array( $this, 'xxx_customTimeFrame' ) );
    }

    public function xxx_customTimeFrame( $schedules ) {
        $schedules['xxx'] = array(
            'interval'   => $userDefinedInterval,
            'display'    => 'XXX Custom Timeframe'
        );

        return $schedules;
    }
}

I have assigned a scheduled tasks using the custom schedule “xxx” like this:

class classB{
    private function scheduleDoingIt(){
        if(wp_next_scheduled('xxx_doIt') == FALSE){
            wp_schedule_event(time(), 'xxx', 'xxx_doIt');
            //I am using the "xxx" custom schedule defined above.
        }

        add_action('xxx_doIt', array($this, 'xxx_doItNow'));
    }

    public function xxx_doItNow(){
        //Dominate the world.
    }
}

I have installed “WP Crontrol” plugin. Using that, I can see the interval of the custom schedule “xxx” is being changed successfully when the user provide a new value for $userDefinedInterval.

However, that doesn’t change the frequency of executing the method xxx_doItNow(). It continues to execute with the original frequency, not the new one that user updated.

Imagine there exists a WordPress function named “wp_get_scheduled_event_frequency()” that returns the actual frequency of the scheduled event in seconds, without referring to the cron schedule it was originally created with (i.e. “xxx”). Then I could do this:

if(wp_get_scheduled_event_frequency('xxx_doIt') == wp_get_schedules()['xxx']['interval']){
    //If the frequency of the scheduled event is different than the interval of cron schedule.
    $timestamp    = //Have to find the next time the scheduled task would have ran.
    $recurrence   = 'xxx';
    $hook         = 'xxx_doIt'
    wp_schedule_event($timestamp, $recurrence, $hook);
}

In other words, if wp_get_schedule('xxx_doIt') could return actual current frequency, rather than the cron schedule name (i.e. ‘xxx’), that would do it.

So, any idea?

Related posts

1 comment

  1. This is an old question, but hopefully this helps someone facing the same issue.

    We can use wp_get_schedule( $hook ) to retrieve the cron schedule for the hook. Then we can use wp_get_schedules() to retrieve the supported cron recurrences. Find the correct array value and return it.

    /**
     * Retrieve Cron interval for hook.
     *
     * @param string $hook Action hook to execute when cron is run.
     * @return int|false False, if no schedule. Interval on success.
     */
    if( ! function_exists( 'wp_get_scheduled_event_frequency' ) ):
    function wp_get_scheduled_event_frequency( $hook ) {
      $schedule  = wp_get_schedule( $hook );
      $schedules = wp_get_schedules();
      return isset( $schedules[ $schedule ] ) ? $schedules[ $schedule ][ 'interval' ] : false;
    }
    endif;
    

Comments are closed.