ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-04 06:18:10  阅读:205  来源: 互联网

标签:wordpress php cron wordpress-plugin


我正在读取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, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', 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, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', 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);
}

标签:wordpress,php,cron,wordpress-plugin
来源: https://codeday.me/bug/20190704/1374175.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有