Cron job in WordPress not working

2019-08-28 23:15发布

I have made a WordPress cron job that should send out a mail each hour:

function mail_cron_job() {
     $time = date( 'h:ia', time() );
       wp_mail( '****@******', 'Hourly E-mail from WP Cron', 'This message was sent on ' . $time );

}

I added this code on a form submission:

if ( !wp_next_scheduled('mail_cron_job') ) {
                wp_schedule_event( time(), 'hourly', 'mail_cron_job');
        }

The form data is submitting OK, but the cron job won't start. No error reports. Anything I'm missing?

2条回答
淡お忘
2楼-- · 2019-08-29 00:12

Cron Job Schedule in wordpress

//Add Interval [ Day ] 
function cron_add_daily($schedules) 
{
// Adds once every minute to the existing schedules.
$schedules['daily'] = array('display' =>( 'Once Daily' ) );
return $schedules;
}
add_filter( 'cron_schedules', 'cron_add_daily' );
// create a scheduled event (if it does not exist already)
function cron_activation() {
 if( !wp_next_scheduled( 'plugin_mailcronjob' ) ) {  
   wp_schedule_event(time(), 'daily', 'plugin_mailcronjob' );  
  }
 }
 // and make sure it's called whenever WordPress loads
  add_action('init', 'crons_activation');

 function repeat_function_daily()
 {
   /*
        put code here what you want check daily
   */

  }
 // hook that function onto our scheduled event:
 add_action ('plugin_mailcronjob', 'repeat_function_daily'); 
查看更多
太酷不给撩
3楼-- · 2019-08-29 00:21

you need to use an action hook to tie the event and function together.

add_action( 'mail_cron_job', 'mail_cron_job' );

You can add that right after the function.

查看更多
登录 后发表回答