wp cron - Check if event was scheduled - schedule event only once

admin2025-06-04  5

I have some programming error in my wordpress plugin. When its running, then it keeps registering cron job to the wordpress. I wrote the plugin as a class:

class Cleverstart_Woofio extends WC_Integration{
    public function __construct(){
       add_action('woofio_hourly', array( $this,'woofio_create_haystack'));
       wp_schedule_event(time(), 'hourly', 'woofio_hourly');
       //other stuff not related to the question
    } 

   public function woofio_create_haystack(){
     //do quite performance heavy stuff
   }

}

$cleverstart_woofio = new Cleverstart_Woofio();

My theory is, that the new Cleverstart_Woofio(); call will always schedule new cron event, clugging my site. I need to check if the event was already scheduled and schedule it only once. However, I am clueless on how to achieve this.

Thanks for help

I have some programming error in my wordpress plugin. When its running, then it keeps registering cron job to the wordpress. I wrote the plugin as a class:

class Cleverstart_Woofio extends WC_Integration{
    public function __construct(){
       add_action('woofio_hourly', array( $this,'woofio_create_haystack'));
       wp_schedule_event(time(), 'hourly', 'woofio_hourly');
       //other stuff not related to the question
    } 

   public function woofio_create_haystack(){
     //do quite performance heavy stuff
   }

}

$cleverstart_woofio = new Cleverstart_Woofio();

My theory is, that the new Cleverstart_Woofio(); call will always schedule new cron event, clugging my site. I need to check if the event was already scheduled and schedule it only once. However, I am clueless on how to achieve this.

Thanks for help

Share Improve this question asked Jan 7, 2019 at 9:33 Pavel JanicekPavel Janicek 2123 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

Have you ever heard of Rubber Ducking?

The official documentation has an answer:

if ( ! wp_next_scheduled( 'woofio_hourly' ) ) {
    wp_schedule_event( time(), 'hourly', 'woofio_hourly' );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749036309a315816.html

最新回复(0)