Sha*_*ngh 5 php wordpress cron
我在Wordpress中执行了多个cron作业。首先,我想清除我已经为这个问题进行了大量搜索,但没有找到确切的解决方案。所以我已经在这里发布了。
问题是一个cron正在运行,而另一个cron从不运行,我已经为第一个cron安排了每三个小时的时间间隔,但是它有时在一分钟之内执行多次以获得此多封邮件。其他永远不会执行。
任何人都可以通过Wordpress Cron提供解决方案,以执行两个功能以不同的固定间隔更新数据库。提前谢谢了。
//The activation hooks is executed when the plugin is activated
register_activation_hook(__FILE__, 'activate_one');
register_activation_hook(__FILE__, 'activate_two');
//Filter for Adding multiple intervals
add_filter( 'cron_schedules', 'intervals_schedule' );
function intervals_schedule($schedules) {
$schedules['threehour'] = array(
'interval' => 10800, // Every 3 hours
'display' => __( 'Every 3 hours' )
);
$schedules['onehour'] = array(
'interval' => 3600, // Every 1 hour
'display' => __( 'Every 1 hour' )
);
return $schedules;
}
//Schedule a first action if it's not already scheduled
function activate_one() {
if (!wp_next_scheduled('cron_action_one')) {
wp_schedule_event( time(), 'threehour', 'cron_action_one');
}
}
//Hook into that action that'll fire threehour
add_action('cron_action_one', 'execute_one');
function execute_one()
{
//Do something or update in database;
}
//Schedule a second action if it's not already scheduled
function activate_two() {
if (!wp_next_scheduled('cron_action_two')) {
wp_schedule_event(time(), 'onehour', 'cron_action_two');
}
}
//Hook into that action that'll fire onehour
add_action('cron_action_two', 'execute_two');
function execute_two()
{
//Do something or update in database;
}
Run Code Online (Sandbox Code Playgroud)
小智 0
最有可能的是,在编写代码和测试时间间隔期间,您已经安排了一些其他cron_action_two事件,这些事件将在很久以后的某个时间调用。您可以使用此处显示的方法之一进行检查。Cron 列表应该让一切变得清晰,并且很可能会解决您的问题。
您的代码中应该修复一些问题,以使其更加稳定并避免此类问题:
清除插件停用时的预定事件,如下所示:
register_deactivation_hook( __FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('my_hourly_event');
}
Run Code Online (Sandbox Code Playgroud)清除激活时的计划挂钩(是:)wp_clear_scheduled_hook( 'my_hourly_event' );而不是检查它是否已经存在(否if( ! wp_next_scheduled( 'my_hourly_event' ) ):)
祝你好运!