此代码将每小时调用您的函数。
// The \'if\' condition is needed to make sure the scheduler runs only once
if ( ! wp_next_scheduled( \'my_custom_action\' ) ){
wp_schedule_event( time(), \'hourly\', \'my_custom_action\' );
}
// Here we create our own action to attach to the scheduler
add_action( \'my_custom_action\', \'update_content\' );
还建议在停用插件时清除所有计划的事件,如下所示:
// The deactivation hook is executed when the plugin is deactivated
register_deactivation_hook( __FILE__, \'my_deactivation\' );
function my_deactivation() {
wp_clear_scheduled_hook( \'my_custom_action\' );
}
请注意,如果您的网站没有访问者,cron将不会运行
here:
您可以阅读更多关于wp_schedule_event()
here.