2014-09-05 42 views
1

方法在一定的時間間隔來執行自定義事件WordPress的定時任務不執行

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

自定義計劃的計劃作業

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,並且它正在列出我的事件,因爲我把每兩分鐘的重現次數顯示出正確的結果,即更新時間+2分鐘。

但我的方法informant_main_action()不起作用。什麼是我犯的錯誤。 ?

感謝

+0

嘗試一個郵件功能,而不是死....你永遠不能確定,如果你會成爲一個觸發cron .... – David 2014-09-05 14:47:53

回答

0

要測試的cron使用類似的郵件().....嘗試每小時測試,然後每週的一個。在插件去激活的情況下銷燬事件是一個好主意(也有一個主題去激活的鉤子,如果你需要它可以查看它),所以如果你需要在不改變鉤子的情況下進行更改,你可以停用該插件名稱。

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