php – WordPress Cron API在后台运行吗?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – WordPress Cron API在后台运行吗?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在读取wp-includes中的cron.PHP代码,而spawn_cron()似乎是实际执行已注册任务代码.

函数的最后两行:

$cron_url = sITe_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );

wp_remote_post( $cron_url,array( 'timeout' => 0.01,'blocking' => false,'sslverify' => apply_filters( 'https_local_ssl_verify',true ) ) );

它只是打开wp-cron.php,将任务作为查询参数传递.

cron.PHP顶部的API描述:

* Schedules a hook which will be executed once by the wordpress actions core at 
* a time which you sPEcify. The action will fire off when someone visits your
* wordpress site,if the schedule time has passed.`

我的问题是,让我们说访问者打开网站的一个页面,然后注册的任务由cron API触发.如果任务很繁重并需要几分钟才能完成,那么在任务完成之前,访问者是否会获得一个未完全加载的页面

[编辑]
为了澄清我的问题,问题是,WP Cron API在页面加载完成后是否运行任务?

解决方法

$cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );
wp_remote_post( $cron_url,true ) ) );

使用下面的示例插件,我确认上面的代码(我在问题中引用)是实际调用计划任务的代码.它设置0.0.1超时并访问wp-cron.php.这意味着如果有100个任务,则加载所有任务需要1秒钟.因此它对页面加载速度有轻微影响.但它似乎不必担心太多.

<?PHP
/* Plugin Name: Sample Cron Task */ 

    // I used `heavy` because this code was initially written to test how it affects the server response if a heavy task is registered as a cron job. So forget about the naming.
add_action('admin_menu','sample_cron_heavy_task');
function sample_cron_heavy_task() {
    add_options_page(
        'Sample Cron Heavy Task','Sample Cron Heavy Task','manage_options','sample_cron_heavy_task','sample_cron_heavy_task_admin');
}
function sample_cron_heavy_task_admin() {
    ?>
    <div class="wrap">
    <?PHP
        wp_schedule_single_event(time(),'my_action_name');
        $cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );
        // executes the registered task immediately 
        wp_remote_post( $cron_url,true ) ) );
        echo get_option('sample_cron_heavy_task');
    ?>
    </div>
    <?PHP
}

add_action('my_action_name','myevent'); 
function myevent() {
    $msg = date('Y m d h:i:s A') . ': the cron task is called.<br />';
    update_option('sample_cron_heavy_task',$msg);
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – WordPress Cron API在后台运行吗?全部内容,希望文章能够帮你解决php – WordPress Cron API在后台运行吗?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。