如何使用服务器 cron 修复未触发的函数



我创建了一个服务器 cron,其中每 30 分钟触发一次函数。问题是数据没有正确发送,但是在单击按钮或我在函数中使用"init"时可以正常工作。此外,cron 正确触发,在需要触发的代码下方添加了发送邮件功能。

function myprefix_custom_cron_schedule( $schedules ) {
    $schedules['every_30_minutes'] = array(
        'interval' => 30 * 60, // Every 30 minutes
        'display'  => __( 'Every 30 minutes' ),
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
    wp_schedule_event( time(), 'every_30_minutes', 'myprefix_cron_hook' );
}
//myprefix_cron_hook
add_action ( 'myprefix_cron_hook', function(){

//this is not being triggered
    $to_create_posts = true;
    $data = RemoteCoin::LoadRemoteCoin( $to_create_posts );

//this is the test email cron (working)
    $to = '***';
    $subject = 'Test my 30-minute cron job';
    $message = 'If you received this message, it means that your 30-minute cron job has worked!'; 
    mail( $to, $subject, $message, 'From: ***' . "com" );
});

但是当单击我创建的按钮时,这是有效的:


if(array_key_exists('mysubmitbtn3',$_POST)){
        $to_create_posts = true;
         $data = RemoteCoin::LoadRemoteCoin( $to_create_posts );
}

此外,这也在使用 init 时起作用:

add_action ( 'init', function(){
    $to_create_posts = true;
    $data = RemoteCoin::LoadRemoteCoin( $to_create_posts );

});

我不确定从哪里开始,因为它没有输出任何错误。任何人都可以帮助我从哪里开始?谢谢。

经过几天的调试,我终于找到了问题所在。它是管理员检查功能(检查触发它的人是否是管理员(,我需要删除它,因为该功能将由服务器触发,而不是个人。

谢谢!

最新更新