wp schedule event() 示例

// register activation hook 
register_activation_hook( __FILE__, 'example_activation' );

// function for activation hook
function example_activation() {
    // check if scheduled hook exists
    if ( !wp_next_scheduled( 'my_event' )) {
         // Schedules a hook
         // time() - the first time of an event to run ( UNIX timestamp format )
         // 'hourly' - recurrence ('hourly', 'twicedaily', 'daily' ) 
         // 'my_event' - the name of an action hook to execute. 
         wp_schedule_event( time(), 'hourly', 'my_event' );
    }
}

add_action( 'my_event', 'do_this_hourly' );

// the code of your hourly event
function do_this_hourly() {
   // put your code here
}

// register deactivation hook 
register_deactivation_hook(__FILE__, 'example_deactivation');

// function for deactivation hook
function example_deactivation() {
    // clear scheduled hook
    wp_clear_scheduled_hook( 'my_event' );
}

重要提示: WordPress cron 僅在你的網站的某個頁面被點選時執行。因此,對於低流量的網站,你需要在主機上設定 cron 來點選頁面。