2

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

1 Answer 1

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' );
}
1
  • Please be aware that this check fails if the hook execution has arguments!
    – Johnny97
    Commented Nov 28, 2023 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.