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 来点击页面。