This is first time that I tried the OOP method of writing an application. I don’t quite understand it yet. Currently I have this cron hook:
if( !wp_next_scheduled( 'my_cron_hook' ) ) {
//schedule the event to run daily
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'my_cron_hook' );
}
add_action('my_cron_hook',array($this,'do_daily_job'));
It seems that it can’t use if
within a class directly. Do I need to put the entire code above into the __construct()
function?
Also, I found this in WordPress wp_schedule_event
documentation.
function my_activation() {
if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event');
}
}
add_action('wp', 'my_activation');
What dose ‘wp’ hook here means? I have never seen this in the hook API. Should I wrapped my cron code (above) in a function and put this ‘wp’ action in the __construct()
function, like this?
function __construct() {
add_action('wp', array($this,'my_activation'));
}
function my_activation() {
if( !wp_next_scheduled( 'my_cron_hook' ) ) {
//schedule the event to run daily
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'my_cron_hook' );
}
add_action('my_cron_hook',array($this,'do_daily_job'));
}