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

3 s
3

Add an action outside your class definition:

add_action('my_unique_plugin_event_hook', array($this,'hook'));

And then use this in your event:

wp_schedule_event(time(), 'daily', 'my_unique_plugin_event_hook');

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *