Wordpress Cron 作业的自定义事件未执行 [英] Custom Event for Wordpress Cron Job Not executing
问题描述
按一定间隔执行的方法
function informant_main_action(){
die('working');
}
Cron 作业的自定义计划
Custom Schedule for Cron Job
function my_cron_definer($schedules){
$schedules['weekly'] = array('interval'=> 120, //604800
'display'=> __('Once Every week')
);
return $schedules;
}
add_filter('cron_schedules','my_cron_definer');
注册插件激活时间表
register_activation_hook( __FILE__, 'informant_run_on_activation' );
function informant_run_on_activation(){
if(!wp_next_scheduled('informantweeklynewsletter')){
wp_schedule_event(time(), 'weekly', 'informantweeklynewsletter');
}
}
调用我的方法在自定义事件上执行
Calling my method to be executed on custom event
add_action('informantweeklynewsletter','informant_main_action');
我安装了一个插件 Cron Gui 来查看预定的 cronjob,它正确地列出了我的事件,因为我每两分钟重复一次,cron gui 显示正确的结果,即它更新时间 +2 分钟.
I have installed a plugin Cron Gui for viewing scheduled cronjob and it is listing my event correctly as i put the recurrence for every two minutes the cron gui shows the correct result i.e its updating the time +2 minutes.
但是我的方法 informant_main_action()
不起作用.我犯了什么错误.?
But my method informant_main_action()
is not working. Whats the mistake i am making. ?
谢谢
推荐答案
要测试 cron,请使用 mail(..... 尝试每小时测试一次,然后每周测试一次.在插件停用时销毁事件是个好主意(还有一个主题停用挂钩,如果需要,请查找它),因此如果您需要进行更改而无需更改挂钩,则可以停用插件姓名.
To test cron use something like mail().....try hourly to test and then your weekly one. Its a good idea to destroy the event on plugin de-activation (there is also a theme de-activation hook, look it up if you need it) so you can de-activate the plugin if you need to make changes without changing the hook name.
add_action('test_event', 'informant_main_action');
function informant_main_action() {
mail('youremailaddress', 'cron running', 'your cronjob has completed');
}
register_activation_hook( __FILE__, 'informant_run_on_activation' );
function informant_run_on_activation() {
if(!wp_next_scheduled('test_event')){
wp_schedule_event(time(), 'hourly', 'test_event');
}
}
register_deactivation_hook( __FILE__, 'deactivate_cron' );
function deactivate_cron() {
wp_clear_scheduled_hook( 'monday_event' );
}
这篇关于Wordpress Cron 作业的自定义事件未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!