WordPress中的Cron工作无法正常工作

时间:2014-09-09 20:13:36

标签: php wordpress cron

我已经制作了一个WordPress cron作业,应该每小时发送一封邮件:

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

}

我在表单提交中添加了此代码:

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

表单数据正在提交,但cron作业无法启动。没有错误报告。我缺少什么?

2 个答案:

答案 0 :(得分:2)

你需要使用一个动作钩子将事件和功能联系在一起。

add_action( 'mail_cron_job', 'mail_cron_job' );

您可以在功能之后添加。

答案 1 :(得分:0)

  

wordpress中的Cron工作时间表

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