dongxiangqian1855 2012-09-24 14:17
浏览 108
已采纳

在WordPress中运行预定的WP Cron任务

I'm trying to run WordPress cron tasks right after scheduling some action events. Usually WordPress Cron at least takes one another page load to trigger scheduled tasks. I'm trying to do it right away. I've almost managed to do it as shown in the below code.

/* Plugin Name: Sample Cron Immediate Execution  */

add_action('admin_menu', 'sample_cron_immediate_execution');
function sample_cron_immediate_execution() {
    add_options_page(
        'Sample Cron Immediate Execution', 
        'Sample Cron Immediate Execution', 
        'manage_options',
        'sample_cron_immediate_execution', 
        'sample_cron_immediate_execution_admin');
}
function sample_cron_immediate_execution_admin() {
    ?>
    <div class="wrap">
    <?php
        wp_schedule_single_event(time(), 'my_action1');
        wp_schedule_single_event(time(), 'my_action2');
        $cron_url = site_url( 'wp-cron.php?doing_wp_cron=0');
        wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
        echo 'cron tasks should be executed by now.';
    ?>
    </div>
    <?php
}

add_action('my_action1', 'myeventfunc1'); 
function myeventfunc1() {
    sleep(10); // assuming it's a heavy task 
    sample_log(date('l jS \of F Y h:i:s A'), __DIR__ . '/myevent1_log.html');
}

add_action('my_action2', 'myeventfunc2');    
function myeventfunc2() {
    sleep(10); // assuming it's a heavy task 
    sample_log(date('l jS \of F Y h:i:s A'), __DIR__ . '/myevent2_log.html');
}

function sample_log($time, $file) {
    file_put_contents($file, $time . '<br />', FILE_APPEND);
}

The problem is that when one of the tasks is heavy, it seems WordPress does not complete all tasks in one call of wp-cron.php. I'm guessing that the $_GET value, doing_wp_cron, is something to do with it as I view the code of the core file,

One of the comments says

Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.

I don't fully understand what this is about. What I'd like to do is to execute multiple scheduled tasks right away without requiring to open another page regardless of the heaviness of the tasks.

Thanks for your information.

  • 写回答

4条回答 默认 最新

  • duanboxue3422 2012-10-02 23:22
    关注

    This should do the job.

    <?php
        /* Plugin Name: Sample Cron Immediate Execution  */
    
        add_action('admin_menu', 'sample_cron_immediate_execution');
        function sample_cron_immediate_execution() {
            add_options_page(
                'Sample Cron Immediate Execution', 
                'Sample Cron Immediate Execution', 
                'manage_options',
                'sample_cron_immediate_execution', 
                'sample_cron_immediate_execution_admin');
        }
        function sample_cron_immediate_execution_admin() {
            ?>
            <div class="wrap">
            <?php
                $cron_url = site_url( '?custom_cron=myeventfunc1');         
                wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
                $cron_url = site_url( '?custom_cron=myeventfunc2');         
                wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
                echo 'cron tasks should be executed by now.';
            ?>
            </div>
            <?php
        }
    
        add_action('init', 'run_customcron');
        function run_customcron() 
        {
            if (isset($_GET['custom_cron']))
            {
                call_user_func($_GET['custom_cron']);
            }
        }
    
        function myeventfunc1() {
            sleep(10); // assuming it's a heavy task 
            sample_log(date('l jS \of F Y h:i:s A'), __DIR__ . '/myevent1_log.html');
        }
    
        function myeventfunc2() {
            sleep(10); // assuming it's a heavy task 
            sample_log(date('l jS \of F Y h:i:s A'), __DIR__ . '/myevent2_log.html');
        }
    
        function sample_log($time, $file) {
            file_put_contents($file, $time . '<br />', FILE_APPEND);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘