Custom Event for Wordpress Cron Job Not executing

2019-08-08 10:47发布

Method to Execute at Certain Interval

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

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 schedule on plugin activation

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

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.

But my method informant_main_action() is not working. Whats the mistake i am making. ?

Thanks

标签: php wordpress
1条回答
乱世女痞
2楼-- · 2019-08-08 11:16

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' );
}
查看更多
登录 后发表回答