【问题标题】:Custom Event for Wordpress Cron Job Not executingWordpress Cron 作业的自定义事件未执行
【发布时间】:2014-09-05 11:21:45
【问题描述】:

按一定间隔执行的方法

function informant_main_action(){
    die('working');
}

Cron 作业的自定义计划

    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');
    }
}

调用我的方法以在自定义事件上执行

add_action('informantweeklynewsletter','informant_main_action');

我已经安装了一个插件 Cron Gui 用于查看计划的 cronjob,它正确列出了我的事件,因为我每两分钟重复一次,cron gui 显示正确的结果,即更新时间 +2 分钟。

但是我的方法informant_main_action() 不起作用。我犯了什么错误。 ?

谢谢

【问题讨论】:

  • 尝试使用邮件功能而不是死机......你永远无法确定你是否会成为触发 cron 的人......

标签: php wordpress


【解决方案1】:

要测试 cron 使用类似 mail() 的东西.....每小时测试一次,然后每周测试一次。在插件停用时销毁事件是一个好主意(还有一个主题停用挂钩,如果需要,请查看它),因此如果您需要在不更改挂钩的情况下进行更改,您可以停用插件姓名。

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' );
}

【讨论】:

    猜你喜欢
    • 2017-01-27
    • 2021-06-03
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多