$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp cron - Check if event was scheduled - schedule event only once|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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' );
}
Post a comment

comment list (0)

  1. No comments so far