wp_schedule_event in a class

This is my (stripped down) plugin, in OOP:

<?php
/*
Plugin Name: Cron Test
Plugin URI: http://www.mywebsite.com/
Description: Test
Version: 1.0
Requires at least: 3.0
*/

class CronTest {
  public function __construct() {
      if ( !wp_next_scheduled( 'dailyops' ) ) {
          wp_schedule_event( time(), 'daily', 'dailyops' );
      }
      add_action( 'dailyops', array($this, 'do_this_daily') );
  }

  function do_this_daily() {
          // do something every day
          error_log('daily');
  }
}

$cronTest = new CronTest();

Anyway, it’s not working (do_this_daily() is never fired). I tried the same in a non-OOP plugin (not based on a class) and it works flawlessy. How can I make it work here, in a class?

EDIT: I replaced the initial portion of the code with a full (still minimal) plugin that can be tested.

1 Answer
1

Finally, I discovered what was wrong: the webserver (a dedicated VPS) had a bad configuration of the hosted domain, so it was unable to “self contact” it, to call wp-cron.php .

I fixed it and now every scheduled job works, including this one 🙂

Leave a Comment